-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.py
207 lines (173 loc) · 6.84 KB
/
user.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from flask import Flask,jsonify,render_template,request,session
from flask import Flask,render_template,url_for,flash,redirect
from forms import RegistrationForm,LoginForm,ResetRequestForm,ResetPassword
app = Flask(__name__)
import random
import geonamescache
import bcrypt
import math
import random
import smtplib
from flask_session import Session
from flask_mysqldb import MySQL
app = Flask(__name__)
mysql = MySQL(app)
import mysql.connector
from terms import terms
gc=geonamescache.GeonamesCache()
countries = gc.get_countries()
from flask import render_template
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
# app.config['MYSQL_PASSWORD'] = 'alchemist'
app.config['MYSQL_PASSWORD'] = 'Zargar@123'
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)
app.config['MYSQL_DB'] = 'ftd'
app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'
mysql = MySQL(app)
#hashing password
def hash_password(password):
# Generate a salt
password = password.encode("utf-8")
hash = bcrypt.hashpw(password, bcrypt.gensalt())
stored_password = hash.decode("utf-8")
return stored_password
# Verify a password against the hashed password in the database
def verify_password(password, hashed_password):
if bcrypt.checkpw(password.encode("utf-8"), hashed_password.encode("utf-8")):
return True
return False
# Register a new user
def register_route():
form = RegistrationForm()
if form.validate_on_submit():
username=form.username.data
email=form.email.data
password=form.password.data
age=form.age.data
region=form.region.data
state=form.state.data
city=form.city.data
gender=form.gender.data
hashed_password = hash_password(password)
# Connect to the database
conn = mysql.connect
cursor = conn.cursor()
# Check if the email already exists
cursor.execute("SELECT * FROM user WHERE email=%s", (email,))
result = cursor.fetchone()
# If email already exists, show error message
if result:
flash('Email already exists, please login to continue.', 'danger')
return redirect(url_for('register'))
# Insert form data into the user table
cursor.execute("INSERT INTO user (username, email, password, age, region, State, City,gender) VALUES (%s, %s, %s, %s, %s, %s, %s,%s)", (username, email, hashed_password, age, region, state, city,gender))
conn.commit()
# Close the cursor and connection
cursor.close()
conn.close()
flash(f'Account created for {username}!', 'success')
return redirect(url_for('home'))
return render_template('register.html', title='Register', form=form)
# Login an existing user
def login_route():
form=LoginForm()
if form.validate_on_submit():
email = form.email.data
password = str(form.password.data)
# Connect to the database
conn = mysql.connect
cursor = conn.cursor()
log=False
# Get the hashed password from the database
cursor.execute("SELECT email FROM user WHERE email=%s", (email,))
result = cursor.fetchone()
# If there is no matching record, show error message
if not result:
flash('Login Unsuccessful. Please check email and password.', 'danger')
return redirect(url_for('login'))
cursor.execute("SELECT password,username FROM user WHERE email=%s", (email,))
result = cursor.fetchone()
hashed_password=str(result[0])
# Verify the entered password with the hashed password
if verify_password(password, hashed_password):
session['logged_in'] = email
session['name']=result[1]
flash('You have been logged in!', 'success')
return redirect(url_for('home'))
else:
flash('Login Unsuccessful. Please check email and password.', 'danger')
return redirect(url_for('login'))
return render_template('login.html', title='Login', form=form)
# Global variable for otp
otp_sent = ""
# function for sending otp to email
def send_otp(email):
digits="0123456789"
OTP=""
for i in range(6):
OTP+=digits[math.floor(random.random()*10)]
global otp_sent
otp_sent = OTP
otp = OTP + " is your OTP"
msg= otp
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login("zargerfasil123@gmail.com", "wnkkbihlwomebczv")
emailid = email
s.sendmail('ftd',emailid,msg)
email_sent = ""
# function to reset password providing email form
def reset_password_route():
form=ResetRequestForm()
if form.validate_on_submit():
email = form.email.data
global email_sent
email_sent=email
# Connect to the database
conn = mysql.connect
cursor = conn.cursor()
# Check if the email exists in the user table
cursor.execute("SELECT * FROM user WHERE email=%s", (email,))
result = cursor.fetchone()
# If there is no matching email, show error message
if not result:
flash('No account with this email found.', 'danger')
return redirect(url_for('reset_password'))
# If email exists, show success message and reset password process
send_otp(email)
flash('Password reset instructions have been sent to your email', 'success')
return redirect(url_for('set_password'))
return render_template('reset_request.html',title='Reset Request',form=form)
# function for setting new password
def set_password_route():
form=ResetPassword()
if form.validate_on_submit():
# get the entered OTP value
entered_otp = form.otp.data
password = form.password.data
# compare the entered OTP with the stored OTP
if entered_otp != otp_sent:
flash('Incorrect OTP. Please try again.', 'danger')
return redirect(url_for('set_password'))
email=email_sent
# If OTP is correct, update the password in the database
conn = mysql.connect
cursor = conn.cursor()
hashed_password=hash_password(password)
cursor.execute("UPDATE user SET password=%s WHERE email=%s", (hashed_password, email))
conn.commit()
flash('Password has been reset successfully.', 'success')
return redirect(url_for('login'))
return render_template('reset.html',title='Reset Request',form=form)
# logout route for expiring session
def logout_route():
# Remove the logged_in key from the session
session.pop('logged_in', None)
return render_template('navbar.html', logged_in=False)
# removing login history after logout
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
return response