Logging users in

Our app can't be complete without the ability to log a user in! In our web_server.py, create a new class, LoginView:

class LoginView(MethodView): 
 
    def get(self): 
        if user_authenticated(): 
            return redirect(url_for('home')) 
        else: 
            return render_template('login.html') 

Like the get in our SignUpView, this one will check to see if the user is already authenticated. If so, then we will redirect them to the home endpoint, otherwise, we will render the login.html template.

At the end of our web_server.py module, add the following URL rule for the LoginView:

app.add_url_rule( 
    '/login', view_func=LoginView.as_view('login') 
) 

Any request to /login will now be directed to our LoginView.

Now create a new template, login.html , inside our ...

Get Python Programming Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.