Skip to content

Commit efeb978

Browse files
committed
Added change password
1 parent f6fcd3d commit efeb978

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

database.db

0 Bytes
Binary file not shown.

main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,35 @@ def editProfile():
130130
conn.close()
131131
return render_template("editProfile.html", profileData=profileData, loggedIn=loggedIn, firstName=firstName, noOfItems=noOfItems)
132132

133+
@app.route("/account/profile/changePassword", methods=["GET", "POST"])
134+
def changePassword():
135+
if 'email' not in session:
136+
return redirect(url_for('loginForm'))
137+
if request.method == "POST":
138+
oldPassword = request.form['oldpassword']
139+
oldPassword = hashlib.md5(oldPassword.encode()).hexdigest()
140+
newPassword = request.form['newpassword']
141+
newPassword = hashlib.md5(newPassword.encode()).hexdigest()
142+
with sqlite3.connect('database.db') as conn:
143+
cur = conn.cursor()
144+
cur.execute("SELECT userId, password FROM users WHERE email = '" + session['email'] + "'")
145+
userId, password = cur.fetchone()
146+
if (password == oldPassword):
147+
try:
148+
cur.execute("UPDATE users SET password = ? WHERE userId = ?", (newPassword, userId))
149+
conn.commit()
150+
msg="Success"
151+
except:
152+
conn.rollback()
153+
msg = "Failure"
154+
return msg
155+
else:
156+
msg = "Wrong password"
157+
conn.close()
158+
return msg
159+
else:
160+
return render_template("changePassword.html")
161+
133162
@app.route("/updateProfile", methods=["GET", "POST"])
134163
def updateProfile():
135164
if request.method == 'POST':

templates/changePassword.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<title>Change Password</title>
4+
<script src={{ url_for('static', filename='js/changePassword.js') }}></script>
5+
</head>
6+
<body>
7+
<form action={{ url_for('changePassword') }} method="POST">
8+
<p>Old Password: <input type="password" name="oldpassword"></p>
9+
<p>New Password: <input type="password" name="newpassword"></p>
10+
<p>Confirm Password: <input type="password" name="cpassword"></p>
11+
<input type="submit" value="Save">
12+
</form>
13+
</body>
14+
</html>
15+
16+

templates/profileHome.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
</div>
4040

4141
<div class="display">
42-
<a href="/account/profile/view">View Profile</a>
43-
<a href="/account/profile/edit">Edit Profile</a>
42+
<a href="/account/profile/view">View Profile</a><br>
43+
<a href="/account/profile/edit">Edit Profile</a><br>
4444
<a href="/account/profile/changePassword">Change password</a>
4545
</div>
4646
</body>

0 commit comments

Comments
 (0)