我正在寫一個金字塔應用程序,允許在數據庫表中註冊任意數量的用戶。這是我的登錄代碼:從金字塔(Python)中的cookie獲取用戶名信息
@view_config(route_name='login', renderer='templates/login.jinja2')
def login(request):
username = request.params.get('username', '')
error = ''
if request.method == 'POST':
error = 'Login Failed'
authenticated = False
try:
authenticated = do_login(request)
except ValueError as e:
error = str(e)
if authenticated:
headers = remember(request, username)
return HTTPFound(request.route_url('home'), headers=headers)
return {'error': error, 'username': username}
其中
def do_login(request):
username = request.params.get('username', None)
password = request.params.get('password', None)
if not (username and password):
raise ValueError('both username and password required')
manager = BCRYPTPasswordManager()
cur = request.db.cursor()
try:
cur.execute("SELECT password FROM users WHERE username=%s", [username])
except psycopg2.Error:
raise ValueError("That username already exists!")
actual_password = cur.fetchall()[0][0] # Extrrrract the data
return manager.check(actual_password, password)
我想一旦給定用戶進行身份驗證,以顯示所有意見的用戶名。我的理解是認證信息存儲在cookie中,並且該cookie看起來像(auth_tkt =「」)。如何從這個cookie獲取「當前」用戶名?
還是我比我意識到更困惑?
PS,我知道我在psycopg2.Error的情況下提出了錯誤的錯誤信息。這只是一個錯字。 –