-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
68 lines (51 loc) · 1.75 KB
/
auth.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
from datetime import date
from flask import Blueprint, render_template, redirect, url_for, request, flash
from flask_login import login_user, logout_user, login_required
from .model import User
auth = Blueprint('auth', __name__)
@auth.route('/login')
def login():
return render_template('login.html')
@auth.route('/login', methods=['POST'])
def login_post():
name = request.form.get('name')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
user = User.query.filter_by(name=name).first()
if not user:
flash('Please check your login details and try again.')
return redirect(url_for('auth.login'))
login_user(user, remember=remember)
return redirect(url_for('main.profile'))
@auth.route('/signup')
def signup():
return render_template('signup.html')
@auth.route('/signup', methods=['POST'])
def signup_post():
name = request.form.get('name')
if not name:
flash('Name field can not be empty')
return redirect(url_for('auth.signup'))
password = request.form.get('password')
phone = request.form.get('phone')
address = request.form.get('address')
user = User.query.filter_by(name=name).first()
if user:
flash('Name already exists')
return redirect(url_for('auth.signup'))
new_user = User()
new_user.name = name
new_user.password = password
new_user.phone = phone
new_user.address = address
new_user.creation_date = date.today()
# add the new user to the database
from . import db
db.session.add(new_user)
db.session.commit()
return redirect(url_for('auth.login'))
@auth.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('main.index'))