-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathauth.py
100 lines (70 loc) · 2.38 KB
/
auth.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from flask import (
Blueprint,
render_template,
session,
request,
redirect,
flash,
url_for
)
from backend.forms import RegisterForm,LoginForm
from backend.models import User
from werkzeug.security import generate_password_hash, check_password_hash
from globals import db
auth = Blueprint("auth",__name__)
@auth.route("/register",methods=["POST","GET"])
def register():
user = session.get("user",None)
user_obj = User.query.filter_by(email=user).first()
form = RegisterForm()
if user_obj!=None:
return redirect(url_for("views.home"))
if request.method == "POST":
if form.validate_on_submit():
if User.query.filter_by(email=form.data["email"]).first() == None:
new_user = User(
name = form.data["name"],
email = form.data["email"],
phone_number = form.data["phone_number"],
password = generate_password_hash(form.data["password"],'sha256')
)
db.session.add(new_user)
db.session.commit()
flash("User created successfully")
return redirect(url_for("auth.login"))
flash("Email already exists")
else:
flash(form.errors)
return render_template(
'register.html',
user = user,
form = form
)
@auth.route('/login',methods=['POST','GET'])
def login():
user = session.get("user",None)
user_obj = User.query.filter_by(email=user).first()
if user_obj!=None:
return redirect(url_for("views.home"))
form = LoginForm()
if request.method == "POST":
if form.validate_on_submit():
user = User.query.filter_by(
email = form.data["email"]
).first()
if user and check_password_hash(user.password,form.data["password"]):
session["user"] = user.email
session["username"] = user.name
return redirect(url_for("views.home"))
flash("Incorrect Credentials")
else:
flash(form.errors)
return render_template(
'login.html',
user = user,
form = form
)
@auth.route("/logout",methods=["GET"])
def logout():
session["user"] = None
return redirect(url_for("auth.login"))