5

I have a flask app with 2 blueprints. auth blueprint and events blueprint. This is how they are structured

`app/
    auth_blueprint/
            init.py
            views.py
            model.py
    events_blueprint/
            init.py
            model.py
            views.py 
`

auth_blueprint handles user events. login,registration etc. My question is, how do I access a token generated from auth_blueprint upon login and use it in the events_blueprint to secure the other endpoints?

in views under auth_blueprint the authentication code is

@authenticate.verify_password
def verify_password(username_or_token, password):
    # first try to authenticate by token
    user = models.User.verify_auth_token(username_or_token)
    if not user:
        # try to authenticate with username/password
        user = models.User.query.filter_by(username = username_or_token).first()
        if not user or not user.verify_password(password):
            return False
    g.user = user

    import pdb; pdb.set_trace() #debugger
    return True

#login user
@auth.route('/api/v1/auth/login')
@authenticate.login_required
def login():    
    token = g.user.generate_auth_token()
    return jsonify({ 'token': token.decode('ascii') })
    #return jsonify({'Welcome':'Login Okay'}),200 

When I try to access g in views under events_blueprint i get an error g has no attribute user

@events.route('/api/v1/user')
def getloggeduser():
    print(g)
    import pdb; pdb.set_trace()
    return g

PS: that is for testing purpose. I am only interested in getting user object stored in g and later on retrieve a userid from there.

I am using jwt for token based authentication. Let me know if anything needs clarification. Thank you

Shammir
  • 927
  • 4
  • 17
  • 32

0 Answers0