问题
I have been doing the Flask Web Development Book by Miguel Grinberg, and just finished up with the authorization blueprint. Though, I am having an issue that if I am already logged in, I can still put the url of the login page and go there. How can I prevent this in Flask?
While using Django, I came up with django-braces library which helps to do this, any such alternative available in Flask?
回答1:
Redirect users who are logged in navigating to the login page.
To check if they are already logged in, examine the current_user proxy. Note that logged-in users will have current_user.is_authenticated() equal True, while users who aren't logged in will have the method return False.
You have two options for the destination: back to their previous page with redirect(request.referrer) or to one of your other pages with redirect(url_for('view_name')).
回答2:
Use current_user.is_authenticated().
来源:https://stackoverflow.com/questions/31059694/flask-how-to-prevent-to-go-to-login-page-when-already-logged-in