Skip to content

Commit 8d26668

Browse files
committed
Added change username and password functionality
1 parent 93c20be commit 8d26668

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

app/routes.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,47 @@ def del_user():
7979
user_db.drop_row("Username", username)
8080
user_db.close_db()
8181
flash(str("User {0} successfully deleted!").format(username))
82-
return render_template("index.html", title='Home')
82+
return render_template("index.html", title='Home')
83+
84+
@app.route('/change_password', methods = ['POST', 'GET'])
85+
def change_password():
86+
if (request.method == 'POST'):
87+
username = request.form['username']
88+
password = request.form['password']
89+
new_password = request.form['new_password']
90+
user_db.open_db()
91+
row = user_db.get_row("Username", username)
92+
93+
try:
94+
if (row != None and row["Username"] == username and sha512_crypt.verify(password, row["Password"]) == True):
95+
user_db.update_row("Password", sha512_crypt.hash(new_password), "Username", username)
96+
user_db.close_db()
97+
flash("Password changed successfully!")
98+
return render_template("account.html", title='Account', email=row["Email"], username=row["Username"], type=row["Type"])
99+
else:
100+
user_db.close_db()
101+
flash("Password change failed! The current password provided is incorrect!")
102+
return render_template("account.html", title='Account', email=row["Email"], username=row["Username"], type=row["Type"])
103+
except:
104+
user_db.close_db()
105+
flash("Password change failed! The current password provided is incorrect!")
106+
return render_template("account.html", title='Account', email=row["Email"], username=row["Username"], type=row["Type"])
107+
108+
@app.route('/change_username', methods = ['POST', 'GET'])
109+
def change_username():
110+
if (request.method == 'POST'):
111+
username = request.form['username']
112+
new_username = request.form['new_username']
113+
user_db.open_db()
114+
115+
if (user_db.get_row("Username", new_username) == None):
116+
user_db.update_row("Username", new_username, "Username", username)
117+
row = user_db.get_row("Username", new_username)
118+
user_db.close_db()
119+
flash("Username changed successfully!")
120+
return render_template("account.html", title='Account', email=row["Email"], username=row["Username"], type=row["Type"])
121+
else:
122+
row = user_db.get_row("Username", username)
123+
user_db.close_db()
124+
flash("Username change failed! That username already exists!")
125+
return render_template("account.html", title='Account', email=row["Email"], username=row["Username"], type=row["Type"])

app/templates/account.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,23 @@ <h1>Account Info</h1>
1010
<input type="hidden" id="username" name="username" value="{{ username }}">
1111
<input type="submit" value="Delete Account">
1212
</form>
13+
14+
<h2>Change Username</h2>
15+
<form action="{{ url_for('change_username') }}" method="POST" accept-charset="UTF-8" autocomplete="off" autofocus="off" enctype="multipart/form-data">
16+
<input type="hidden" id="username" name="username" value="{{ username }}" required/></br>
17+
<label for="new_username">New Username</label>
18+
<input type="text" id="new_username" name="new_username" size="32" minlength="8" maxlength="32" required/></br>
19+
<input type="submit" value="Submit"/>
20+
</form>
21+
22+
<h2>Change Password</h2>
23+
<form action="{{ url_for('change_password') }}" method="POST" accept-charset="UTF-8" autocomplete="off" autofocus="off" enctype="multipart/form-data">
24+
<input type="hidden" id="username" name="username" value="{{ username }}" required/></br>
25+
<label for="password">Password</label>
26+
<input type="password" id="password" name="password" size="32" minlength="8" maxlength="32" required/></br>
27+
<label for="new_password">New Password</label>
28+
<input type="password" id="new_password" name="new_password" size="32" minlength="8" maxlength="32" required/></br>
29+
<input type="submit" value="Submit"/>
30+
</form>
1331
</div>
1432
{% endblock %}

app/templates/login.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<div class="box">
55
<h1>Login</h1>
66
<form action="{{ url_for('login_user') }}" method="POST">
7-
<p> {{ form.username.label }} <br> {{ form.username(size=32, autocomplete="off", autofocus="off") }}</p>
8-
<p> {{ form.password.label }} <br> {{ form.password(size=32, autocomplete="off", autofocus="off") }}</p>
7+
<p> {{ form.username.label }} <br> {{ form.username(size=32, minlength="8", maxlength="32", autocomplete="off", autofocus="off") }}</p>
8+
<p> {{ form.password.label }} <br> {{ form.password(size=32, minlength="8", maxlength="32", autocomplete="off", autofocus="off") }}</p>
99
<p> {{ form.submit() }} </p>
1010
</form>
1111
</div>

app/templates/register.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<h1>Register</h1>
66
<form action="{{ url_for('register_user') }}" method="POST">
77
<p> {{ form.email.label }} <br> {{ form.email(size=32, autocomplete="off", autofocus="off") }}</p>
8-
<p> {{ form.username.label }} <br> {{ form.username(size=32, autocomplete="off", autofocus="off") }}</p>
9-
<p> {{ form.password.label }} <br> {{ form.password(size=32, autocomplete="off", autofocus="off") }}</p>
8+
<p> {{ form.username.label }} <br> {{ form.username(size=32, minlength="8", maxlength="32", autocomplete="off", autofocus="off") }}</p>
9+
<p> {{ form.password.label }} <br> {{ form.password(size=32, minlength="8", maxlength="32", autocomplete="off", autofocus="off") }}</p>
1010
<p> {{ form.submit() }} </p>
1111
</form>
1212
</div>

0 commit comments

Comments
 (0)