moviedb/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
from flask import (Blueprint, render_template, redirect, url_for, request, flash) from werkzeug.security import generate_password_hash, check_password_hash from .models import user_info, fav from . import db, session, g auth = Blueprint('auth', __name__) @auth.route('/login') def login(): newuser = request.args.get('nu', None) return render_template("login.html", newuser=newuser).replace( '<html lang="en"', '<html lang="en" style="background-image:url(../static/img/bg.jpg)"' ,1) @auth.route('/login', methods=['POST']) def login_post(): username = request.form.get('username') password = request.form.get('password') remember = True if request.form.get('remember') else False user = user_info.query.filter_by(username=username).first() if not user or not check_password_hash(user.password, password): flash('Please check your login details and try again.') return redirect(url_for('auth.login')) else: session['user'] = username g.user = session['user'] return redirect(url_for('main.profile')) @auth.route('/signup') def signup(): return render_template("signup.html").replace('<html lang="en"', '<html lang="en" style="background-image:url(../static/img/bg.jpg)"' ,1) @auth.route('/signup', methods=['POST']) def signup_post(): username = request.form.get('username') password = request.form.get('password') if not username or not password: flash("Username or Password cannot be emtpy") return redirect(url_for('auth.signup')) user = user_info.query.filter_by(username=username).first() if user: flash('Username already exists') return redirect(url_for('auth.signup')) new_user = user_info(username=username, password=generate_password_hash( password, method='sha256')) db.session.add(new_user) db.session.commit() return redirect(url_for('auth.login', nu=1)) @auth.route('/logout') def logout(): session['user'] = None return redirect(url_for('auth.login'))