authentication - Get username information from a cookie in Pyramid (Python) -


i'm writing pyramid app allows registration of arbitrary number of users in database table. login code:

@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 e:             error = str(e)         if authenticated:             headers = remember(request, username)             return httpfound(request.route_url('home'), headers=headers)     return {'error': error, 'username': username} 

where

def do_login(request):     username = request.params.get('username', none)     password = request.params.get('password', none)     if not (username , password):         raise valueerror('both username , password required')     manager = bcryptpasswordmanager()     cur = request.db.cursor()     try:         cur.execute("select password users username=%s", [username])     except psycopg2.error:         raise valueerror("that username exists!")     actual_password = cur.fetchall()[0][0]  # extrrrract data     return manager.check(actual_password, password) 

i want display username on views once given user authenticated. understanding authentication information stored in cookie, , cookie looks (auth_tkt=""). how "current" username cookie?

or more confused realize?

you can authenticated username calling request.authenticated_userid. can find more in official docs.

i tend store whole user object (and dbsession) in request this:

def includeme(config):     .models.user import user      settings = config.get_settings()     dbsession_factory = get_dbsession_factory(settings)     config.add_request_method(         lambda request: dbsession_factory(),         'dbsession',         reify=true)     config.add_request_method(         lambda request: user.get_by_username(             request.dbsession,             request.authenticated_userid),         'user',         reify=true)  def get_dbsession_factory(settings):     engine = engine_from_config(settings, 'sqlalchemy.')     dbsession_factory = sessionmaker()     register(dbsession_factory)     dbsession_factory.configure(bind=engine)     return dbsession_factory  

then call config.include('your_app.models') in app __init__.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -