0

I haven't been able to pass the return values of a @app.before_request function to the endpoint's function.

I want the app to be authentication first and add decorators in case authentication is not required.

Hope you guys can help me sort this one out.

@app.before_request
def check_authentication_user():
    if request.endpoint in _insecure_views:
        print("Skipped authentication")
        return
    
    try:
        cookieVal = request.cookies.get('usrCredentials')
        if not cookieVal:
            resp = make_response(redirect('/login'))
            return resp
        # Some code
        return cookieVal # I need to pass this return to the route's function
    except (InvalidToken):
        resp = make_response(redirect('/login'))
        return resp

def skip_authentication(fn):
    '''decorator to disable user authentication'''
    endpoint = fn.__name__
    _insecure_views.append(endpoint)
    return fn


@app.route('/')
@skip_authentication
def index(cookieVal):
    print("cookieVal: ", cookieVal)
    ### Some code with cookie val###
    return "data", 200

I think if there's a way to treat the before_request as a wrapper to the endpoint's function it would work but I'm new to wrappers and have not been able to do so.

Also I know about the flask g variable but by using g I would have to check the data on the variables at every endpoint instead of the getting it directly in the parameters

0 Answers0