|
| 1 | +# -*- encoding: utf-8 -*- |
| 2 | +""" |
| 3 | +Copyright (c) 2019 - present AppSeed.us |
| 4 | +""" |
| 5 | + |
| 6 | +from flask import render_template, redirect, request, url_for |
| 7 | +from flask_login import ( |
| 8 | + current_user, |
| 9 | + login_user, |
| 10 | + logout_user |
| 11 | +) |
| 12 | + |
| 13 | +from apps import db, login_manager |
| 14 | +from apps.authentication import blueprint |
| 15 | +from apps.authentication.forms import LoginForm, CreateAccountForm |
| 16 | +from apps.authentication.models import Users |
| 17 | + |
| 18 | +from apps.authentication.util import verify_pass |
| 19 | + |
| 20 | + |
| 21 | +@blueprint.route('/') |
| 22 | +def route_default(): |
| 23 | + return redirect(url_for('authentication_blueprint.login')) |
| 24 | + |
| 25 | + |
| 26 | +# Login & Registration |
| 27 | + |
| 28 | +@blueprint.route('/login', methods=['GET', 'POST']) |
| 29 | +def login(): |
| 30 | + login_form = LoginForm(request.form) |
| 31 | + if 'login' in request.form: |
| 32 | + |
| 33 | + # read form data |
| 34 | + username = request.form['username'] |
| 35 | + password = request.form['password'] |
| 36 | + |
| 37 | + # Locate user |
| 38 | + user = Users.query.filter_by(username=username).first() |
| 39 | + |
| 40 | + # Check the password |
| 41 | + if user and verify_pass(password, user.password): |
| 42 | + |
| 43 | + login_user(user) |
| 44 | + return redirect(url_for('authentication_blueprint.route_default')) |
| 45 | + |
| 46 | + # Something (user or pass) is not ok |
| 47 | + return render_template('accounts/login.html', |
| 48 | + msg='Wrong user or password', |
| 49 | + form=login_form) |
| 50 | + |
| 51 | + if not current_user.is_authenticated: |
| 52 | + return render_template('accounts/login.html', |
| 53 | + form=login_form) |
| 54 | + return redirect(url_for('home_blueprint.index')) |
| 55 | + |
| 56 | + |
| 57 | +@blueprint.route('/register', methods=['GET', 'POST']) |
| 58 | +def register(): |
| 59 | + create_account_form = CreateAccountForm(request.form) |
| 60 | + if 'register' in request.form: |
| 61 | + |
| 62 | + username = request.form['username'] |
| 63 | + email = request.form['email'] |
| 64 | + |
| 65 | + # Check usename exists |
| 66 | + user = Users.query.filter_by(username=username).first() |
| 67 | + if user: |
| 68 | + return render_template('accounts/register.html', |
| 69 | + msg='Username already registered', |
| 70 | + success=False, |
| 71 | + form=create_account_form) |
| 72 | + |
| 73 | + # Check email exists |
| 74 | + user = Users.query.filter_by(email=email).first() |
| 75 | + if user: |
| 76 | + return render_template('accounts/register.html', |
| 77 | + msg='Email already registered', |
| 78 | + success=False, |
| 79 | + form=create_account_form) |
| 80 | + |
| 81 | + # else we can create the user |
| 82 | + user = Users(**request.form) |
| 83 | + db.session.add(user) |
| 84 | + db.session.commit() |
| 85 | + |
| 86 | + return render_template('accounts/register.html', |
| 87 | + msg='User created please <a href="/login">login</a>', |
| 88 | + success=True, |
| 89 | + form=create_account_form) |
| 90 | + |
| 91 | + else: |
| 92 | + return render_template('accounts/register.html', form=create_account_form) |
| 93 | + |
| 94 | + |
| 95 | +@blueprint.route('/logout') |
| 96 | +def logout(): |
| 97 | + logout_user() |
| 98 | + return redirect(url_for('authentication_blueprint.login')) |
| 99 | + |
| 100 | + |
| 101 | +# Errors |
| 102 | + |
| 103 | +@login_manager.unauthorized_handler |
| 104 | +def unauthorized_handler(): |
| 105 | + return render_template('home/page-403.html'), 403 |
| 106 | + |
| 107 | + |
| 108 | +@blueprint.errorhandler(403) |
| 109 | +def access_forbidden(error): |
| 110 | + return render_template('home/page-403.html'), 403 |
| 111 | + |
| 112 | + |
| 113 | +@blueprint.errorhandler(404) |
| 114 | +def not_found_error(error): |
| 115 | + return render_template('home/page-404.html'), 404 |
| 116 | + |
| 117 | + |
| 118 | +@blueprint.errorhandler(500) |
| 119 | +def internal_error(error): |
| 120 | + return render_template('home/page-500.html'), 500 |
0 commit comments