Skip to content

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

Open
@MitchvanWijngaarden

Description

@MitchvanWijngaarden
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions