How does Request Loader in flask-login customize login?

1. Flask-login is used for user login in flask, but how to make flask-login support support for both token and Authorization header login?
the official document says that it supports token and Authorization header, but it is not very clear how to use it. Can it support three verification methods at the same time?

@login_manager.request_loader
def load_user_from_request(request):

    -sharp first, try to login using the api_key url arg
    api_key = request.args.get("api_key")
    if api_key:
        user = User.query.filter_by(api_key=api_key).first()
        if user:
            return user

    -sharp next, try to login using Basic Auth
    api_key = request.headers.get("Authorization")
    if api_key:
        api_key = api_key.replace("Basic ", "", 1)
        try:
            api_key = base64.b64decode(api_key)
        except TypeError:
            pass
        user = User.query.filter_by(api_key=api_key).first()
        if user:
            return user

    -sharp finally, return None if both methods did not login the user
    return None
Mar.03,2021

Request Loader Custom Login


use the flask-httpauth plug-in with token authentication

Menu