-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (47 loc) · 1.85 KB
/
app.py
File metadata and controls
54 lines (47 loc) · 1.85 KB
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
from flask import Flask, render_template, request, redirect, url_for, session
from pymongo import MongoClient
from hashlib import sha256
import config
import certifi
import uuid
app = Flask(__name__)
app.config.from_object(config)
client = MongoClient(config.MONGO_URI,tlsCAFile=certifi.where())
db = client["Userdatabase"]
col = db["users"]
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = col.find_one({'username': username})
if user and sha256(password.encode("utf-8")).hexdigest()==user['password']:
session['username'] = username
return redirect(url_for('welcome'))
else:
return render_template('login.html', error='Invalid username or password')
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
confirm_password = request.form['confirm_password']
if password == confirm_password:
hashed_password = sha256(password.encode("utf-8")).hexdigest()
col.insert_one({'_id':uuid.uuid4().hex,'username': username, 'password': hashed_password})
return redirect(url_for('login'))
else:
return render_template('register.html', error='Passwords do not match')
return render_template('register.html')
@app.route('/welcome')
def welcome():
if 'username' in session:
return render_template('welcome.html', username=session['username'])
return redirect(url_for('login'))
if __name__ == '__main__':
app.secret_key = 'your_secret_key'
app.run(debug=True)