Flask-security login and logout

rottentomato56 picture rottentomato56 · Oct 22, 2013 · Viewed 9.1k times · Source

How do you log a user in and out in Flask using the Flask-Security extension?

I just started using flask-security, and going through the documentation here http://pythonhosted.org/Flask-Security/api.html , I can't really figure out how to explicitly log a user in and out, as well as authenticate the user before logging them in.

Answer

codegeek picture codegeek · Oct 22, 2013

Flask-Security uses Flask-Login. So you can do the following as per documentation:

login_user(user) # where user is your user object

logout_user()

Obviously, you still need to write your views where you will validate the form submissions etc before calling these methods and do the proper template rendering or redirects as needed.