Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (57 loc) · 2.52 KB
/
Copy pathapp.py
File metadata and controls
110 lines (57 loc) · 2.52 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
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
from flask import Flask, render_template, flash, redirect, request, session, logging, url_for
from flask_sqlalchemy import SQLAlchemy
from forms import LoginForm, RegisterForm
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.config['SECRET_KEY'] = '!9m@S-dThyIlW[pHQbN^'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@localhost/auth'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name= db.Column(db.String(15), unique=True)
username = db.Column(db.String(15), unique=True)
email = db.Column(db.String(50), unique=True)
password = db.Column(db.String(256), unique=True)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/login/', methods = ['GET', 'POST'])
def login():
form = LoginForm(request.form)
if request.method == 'POST' and form.validate:
user = User.query.filter_by(email = form.email.data).first()
if user:
if check_password_hash(user.password, form.password.data):
flash('You have successfully logged in.', "success")
session['logged_in'] = True
session['email'] = user.email
session['username'] = user.username
return redirect(url_for('home'))
else:
flash('Username or Password Incorrect', "Danger")
return redirect(url_for('login'))
return render_template('login.html', form = form)
@app.route('/register/', methods = ['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
hashed_password = generate_password_hash(form.password.data, method='sha256')
new_user = User(
name = form.name.data,
username = form.username.data,
email = form.email.data,
password = hashed_password)
db.session.add(new_user)
db.session.commit()
flash('You have successfully registered', 'success')
return redirect(url_for('login'))
else:
return render_template('register.html', form = form)
@app.route('/logout/')
def logout():
session['logged_in'] = False
return redirect(url_for('home'))
if __name__ == '__main__':
db.create_all()
app.run(debug=True)