-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
53 lines (47 loc) · 1.9 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import render_template, request, redirect, flash, url_for
from flask_login import login_user, logout_user, current_user, login_required
from models import User
from sqlalchemy.exc import IntegrityError
def register_routes(app, db, bcrypt):
@app.route("/")
def index():
return render_template("index.html", user=current_user)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
user = User.query.filter_by(username=username).first()
if user and bcrypt.check_password_hash(user.password, password):
login_user(user)
return redirect(url_for("protected"))
flash("Invalid username or password")
return render_template("login.html")
@app.route("/signup", methods=["GET", "POST"])
def signup():
if request.method == "POST":
username = request.form["username"]
name = request.form["name"]
password = request.form["password"]
email = request.form["email"]
hashed_password = bcrypt.generate_password_hash(password).decode("utf-8")
new_user = User(
name=name, username=username, email=email, password=hashed_password
)
try:
db.session.add(new_user)
db.session.commit()
return redirect(url_for("login"))
except IntegrityError:
db.session.rollback()
flash("Username or email already exists")
return render_template("signup.html")
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for("index"))
@app.route("/protected")
@login_required
def protected():
return render_template("protected.html")