2019-07-05 12:47:35 +00:00
|
|
|
from flask import flash, redirect, render_template, request, url_for
|
|
|
|
from flask_login import login_required, login_user, logout_user
|
2019-07-04 13:17:51 +00:00
|
|
|
from . import auth
|
2019-07-05 12:47:35 +00:00
|
|
|
from .forms import LoginForm
|
|
|
|
from ..models import User
|
2019-07-04 13:17:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@auth.route('/login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
2019-07-05 12:47:35 +00:00
|
|
|
form = LoginForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
user = User.query.filter_by(email=form.email.data).first()
|
|
|
|
if user is not None and user.verify_password(form.password.data):
|
|
|
|
login_user(user, form.remember_me.data)
|
|
|
|
next = request.args.get('next')
|
|
|
|
if next is None or not next.startswith('/'):
|
|
|
|
next = url_for('main.index')
|
|
|
|
return redirect(next)
|
|
|
|
flash('Invalid username or password.')
|
|
|
|
return render_template('auth/login.html.j2', form=form, title='Log in')
|
|
|
|
|
|
|
|
|
|
|
|
@auth.route('/logout')
|
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
flash('You have been logged out.')
|
|
|
|
return redirect(url_for('main.index'))
|
2019-07-04 13:17:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@auth.route('/register', methods=['GET', 'POST'])
|
|
|
|
def register():
|
|
|
|
return render_template('auth/register.html.j2')
|