Flask: How to prevent to go to login page when already logged in?

我与影子孤独终老i 提交于 2019-12-23 05:36:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!