-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
108 lines (72 loc) · 3.23 KB
/
index.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
101
102
103
104
105
106
107
108
from flask import Flask, request, render_template, redirect, url_for
from db import *
from verify import sendEmail
import string
import random
app = Flask(__name__, template_folder="html")
def randomHash():
return ''.join(random.choices(string.digits, k=4))
@app.route("/", methods=['GET', 'POST'])
def index():
return render_template("index.html")
@app.route("/login", methods=['POST', 'GET'])
def login():
if request.method == "POST":
email, password = request.form["email"], request.form["password"]
if emailExist(email):
account_password = getPassword(email)
if account_password == password:
return redirect(url_for("success"))
elif account_password != password:
return render_template("login.html", error="Password not correct!")
elif not emailExist(email):
return render_template("login.html", error="Email does not exist")
return render_template("login.html")
@app.route("/register", methods=['GET', 'POST'])
def register():
if request.method == "POST":
email, password = request.form['email'], request.form['password']
if len(email) > 5 and len(password) > 5:
if not emailExist(email):
createAccount(email, password)
return redirect(url_for("login"))
elif emailExist(email):
return render_template("register.html", error="Email already exists")
else:
return render_template("register.html", error="Please input an email and password bigger than 5 characters")
return render_template("register.html")
@app.route("/forgot-password", methods=['GET', 'POST'])
def forgot_password():
if request.method == "POST":
email = request.form['email']
if emailExist(email):
forgot_hash = randomHash()
addEmailHash(email, forgot_hash)
hash_link = "http://127.0.0.1:5000/reset-password?pin=" + forgot_hash
sendEmail(email, hash_link)
return render_template("forgot-password.html", msg="password reset link has been sent")
elif not emailExist(email):
return render_template("forgot-password.html", error="Email does not exist")
return render_template("forgot-password.html")
@app.route("/reset-password", methods=['GET', 'POST'])
def resetPassword():
hash = request.args['pin']
if request.method == "GET":
if getEmailFromHash(hash):
return render_template("resetpassword.html")
elif not getEmailFromHash(hash):
return "<code>hash is not valid</code>"
elif request.method == "POST":
new_password, new_password2 = request.form['new_password'], request.form['new_password2']
if new_password != new_password2:
return render_template("resetpassword.html", error="Passwords do not match!")
elif new_password == new_password2:
account_email = getEmailFromHash(hash)
changePassword(account_email, new_password)
removeHashEntry(hash)
return redirect(url_for("login"))
@app.route("/success")
def success():
return render_template("success.html")
if __name__ == '__main__':
app.run()