Flask Login & Flask Logout Tutorial

In this Flask tutorial we are going to learn about Flask Login & Flask Logout Tutorial , so for

this we are using a Flask Extension that is called Flask-Login. Flask-Login provides user

session management for Flask. It handles the common tasks of logging in, logging out,

and remembering your users’ sessions over extended periods of time. you can check

Flask-Login documentation for more. 

 

 

 

Installation 

Install the extension with pip:

 

 

 

The most important part of an application that uses Flask-Login is the LoginManager

class. You should create one for your application somewhere in your code, for example

we are using the LoginManager in our app.py file.

 

 

Also you need to provide  a user_loader callback. This callback is used to

reload the user object from the user ID stored in the session.

 

 

 

So now let’s add the complete code for app.py. 

 

 

 

 

OK in the app.y file , these are the imports that we need for Flask Login & Flask Logout Tutorial.

 

 

 

This is Flask SQLAlchemy configuration for mysql database, we have already covered this

article, you can check this link Flask SQLAlchemy with Mysql . make sure that you have

created a database in the Wamp Server, my database name is flaskcodeloop .

 

 

 

 

Also we need to create LoginManager object, and initialize our LoginManager.

 

 

 

 

This is our database model that we have already covered in Flask WTF Forms .

 

 

 

 

 

As i have said that you need to provide  a user_loader callback. This

callback is used to reload the user object from the user ID stored in the session.

 

 

 

 

Also in our index route we have used login_required decorator, now no one can access

our index page without login. and in here you can see we are getting the current logged

in username. we render this username in our index.html.

 

 

 

 

 

This is our login route, in this route we are checking the user against our database registered

users, if the credentials were valid, we login the user and redirect the user to the index page,

if user credentials were not valid we are giving a flash message.

 

 

 

 

 

And this is our logout view.

 

 

 

 

Our forms.py file, we have just our LoginForm and RegistrationForm.

 

 

 

 

Note : You need to use this command for creating your model table in the database.

 

 

 

 

 

Our base.html file, also we have added our login and signup buttons with logout link.

 

 

templates/base.html

 

 

 

 

 

This is our registration.html, and we have simply rendered our form with our Flash Message.

 

 

templates/registration.html

 

 

 

 

 

In here we have login.html file.

 

 

templates/login.html

 

 

 

 

 

This is our index.html.

 

templates/index.html

 

 

 

 

 

 

Now run your Flask Project and go to http://localhost:5000/, you can see that

we can not access to index page and it is redirecting us to the login page.

Flask Login & Flask Logout Tutorial
Flask Login & Flask Logout Tutorial

 

 

 

 

 

 

If you give your valid credentials, you will be redirected to the index page.

 

Flask Login & Flask Logout Tutorial
Flask Login & Flask Logout Tutorial

 

 

 

 

 

 

Also you can watch my complete 4 hours training on Flask Web Development.

Subscribe and Get Free Video Courses & Articles in your Email

 

Comments are closed.

Codeloop
Share via
Copy link
Powered by Social Snap
×