1

My load_user function is like:

@login_manager.user_loader
def load_user(user_id):
    u = None
    # user_id is represented as a json-formatted string
    try:
        user = json.loads(user_id)
        u = load_user_from_backend(user.get('user_id'), user.get('password'))
    except:
        return None
    return u

I want the current user to be redirected back to the login page when laod_user return a None value. How do I achieve this? Thanks.

dibugger
  • 546
  • 1
  • 7
  • 21

1 Answers1

1

Nothing that an if statement can't handle. Certainly not the best approach to handle user authentication, though.

result = load_user(user_id)
if result is None:
    return redirect(url_for('login'))
Gabe
  • 956
  • 7
  • 8
  • It may worth turning this into a decorator for a slightly more portable but far from a comprehensive solution – airstrike Apr 12 '19 at 02:09