Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get current authenticated user on protected route? #18

Open
MitchvanWijngaarden opened this issue Dec 5, 2019 · 1 comment
Open

Comments

@MitchvanWijngaarden
Copy link

def token_required(function):
    @wraps(function)
    def decorated(*args, **kwargs):

        data, status = Auth.get_logged_in_user(request)
        token = data.get('data')

        if not token:
            return data, status

        return function(*args, **kwargs)

    return decorated
@api.route('/<intra_id>')
@api.param('intra_id', 'The intranet identifier')
@api.response(404, 'Intranet not found.')
class Intranet(Resource):

    @api.doc(security='Bearer Auth')
    @api.doc('get a intranet')
    @api.marshal_with(_intranet)
    @token_required
    def get(self, intra_id):
        intranet = get_intranet(intra_id)
        if not intranet:
            api.abort(404)
        else:
            return intranet

@token_required forces a valid Authorization key, but this decorator cannot be accesed to get the token object (containing user_id, e-mail, is administrator etc.).
I can return the token object by passing it to **kwargs in the decorated function using

kwargs['token]' = token

but I'm not sure if this is the proper way to do it.

How do I properly get the token object from the decorator?

@MitchvanWijngaarden
Copy link
Author

I have found an additional method to get the User object:

@token_required
def get(self, intra_id):
     from flask import request
     user = Auth.get_logged_in_user(request)

Is this the correct way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant