from flask import Blueprint, render_template
from flask_login import login_required

main = Blueprint("main", __name__)


@main.route("/")
@main.route("/index")
@login_required
def index():
    return "Logged in"


@main.route("/login")
def login():
    return render_template("login.html")
from flask import Blueprint, render_template, g, redirect, url_for
from flask_login import login_required, current_user, logout_user

main = Blueprint("main", __name__)


@main.route("/")
@main.route("/index")
@login_required
def index():
    return "Logged in"


@main.route("/login")
def login():
    if g.user.is_authenticated:
        return redirect(url_for("main.index"))

    return render_template("login.html")


@main.route("/logout")
def logout():
    logout_user()
    return redirect(url_for("main.login"))


@main.before_request
def before_request():
    g.user = current_user
Add logout and auth checks
Add logout and auth checks

- from flask import Blueprint, render_template
+ from flask import Blueprint, render_template, g, redirect, url_for
- from flask_login import login_required
+ from flask_login import login_required, current_user, logout_user
  
  main = Blueprint("main", __name__)
  
  
  @main.route("/")
  @main.route("/index")
  @login_required
  def index():
      return "Logged in"
  
  
  @main.route("/login")
  def login():
+     if g.user.is_authenticated:
+         return redirect(url_for("main.index"))
+ 
      return render_template("login.html")
+ 
+ 
+ @main.route("/logout")
+ def logout():
+     logout_user()
+     return redirect(url_for("main.login"))
+ 
+ 
+ @main.before_request
+ def before_request():
+     g.user = current_user
Add logout and auth checks
## Code Before:
from flask import Blueprint, render_template
from flask_login import login_required

main = Blueprint("main", __name__)


@main.route("/")
@main.route("/index")
@login_required
def index():
    return "Logged in"


@main.route("/login")
def login():
    return render_template("login.html")
## Instruction:
Add logout and auth checks
## Code After:
from flask import Blueprint, render_template, g, redirect, url_for
from flask_login import login_required, current_user, logout_user

main = Blueprint("main", __name__)


@main.route("/")
@main.route("/index")
@login_required
def index():
    return "Logged in"


@main.route("/login")
def login():
    if g.user.is_authenticated:
        return redirect(url_for("main.index"))

    return render_template("login.html")


@main.route("/logout")
def logout():
    logout_user()
    return redirect(url_for("main.login"))


@main.before_request
def before_request():
    g.user = current_user