-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
77 lines (55 loc) · 1.9 KB
/
app.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
"""
Flask Auth
This is a demonstration of how authentication and authorization works. There are also tools in
the python and flask communities that you can use. Study the following:
- models.py: Shows how the user passwords are created and stored
- views.py: Shows how authorization is setup using the session
- _base.html: Shows how authorization is used in the view.
But you should understand how it's generally working!
"""
from flask import Flask, flash, render_template, redirect, request, session
from models import User, connect_to_db
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login')
def login_view():
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login():
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter_by(email=email).first()
if user.is_valid_password(password):
session['user_id'] = user.id
flash("Login success!")
return redirect('/')
flash("Login failed. Try again.")
return render_template('login.html')
@app.route('/logout')
def logout():
session['user_id'] = None
return render_template('index.html')
@app.route('/signup')
def signup():
return render_template('signup.html')
@app.route('/signup', methods=['POST'])
def register():
email = request.form.get('email')
name = request.form.get('name')
password = request.form.get('password')
conf_pw = request.form.get('confirm_password')
if password == conf_pw:
user = User(email=email, name=name)
user.create_password(password)
user.save()
session['user_id'] = user.id
return redirect('/')
else:
flash('Your passwords do not match.')
return render_template('signup.html')
if __name__ == '__main__':
app.secret_key = 'secretzzzz'
connect_to_db(app)
app.run()