@ -0,0 +1,11 @@ | |||||
from flask_wtf import FlaskForm | |||||
from wtforms import StringField, PasswordField, BooleanField, SubmitField | |||||
from wtforms.validators import DataRequired, Length, Email | |||||
class LoginForm(FlaskForm): | |||||
email = StringField('Email', validators=[DataRequired(), Length(1, 64), | |||||
Email()]) | |||||
password = PasswordField('Password', validators=[DataRequired()]) | |||||
remember_me = BooleanField('Keep me logged in') | |||||
submit = SubmitField('Log in') |
@ -1,7 +1,28 @@ | |||||
from flask import render_template | |||||
from flask import render_template, redirect, request, url_for, flash | |||||
from flask_login import login_user, logout_user, login_required | |||||
from . import auth | from . import auth | ||||
from ..models import User | |||||
from .forms import LoginForm | |||||
@auth.route('/login') | |||||
@auth.route('/login', methods=['GET', 'POST']) | |||||
def login(): | def login(): | ||||
return render_template('auth/login.html') | |||||
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', form=form) | |||||
@auth.route('/logout') | |||||
@login_required | |||||
def logout(): | |||||
logout_user() | |||||
flash('You have been logged out.') | |||||
return redirect(url_for('main.index')) |
@ -1,29 +1,7 @@ | |||||
from flask import render_template, session, redirect, url_for, current_app | |||||
from .. import db | |||||
from ..models import User | |||||
from ..email import send_email | |||||
from flask import render_template | |||||
from . import main | from . import main | ||||
from .forms import NameForm | |||||
@main.route('/', methods=['GET', 'POST']) | @main.route('/', methods=['GET', 'POST']) | ||||
def index(): | def index(): | ||||
form = NameForm() | |||||
if form.validate_on_submit(): | |||||
user = User.query.filter_by(username=form.name.data).first() | |||||
if user is None: | |||||
user = User(username=form.name.data) | |||||
db.session.add(user) | |||||
db.session.commit() | |||||
session['known'] = False | |||||
if current_app.config['FLASKY_ADMIN']: | |||||
send_email(current_app.config['FLASKY_ADMIN'], 'New user', | |||||
'mail/new_user', user=user) | |||||
else: | |||||
session['known'] = True | |||||
session['name'] = form.name.data | |||||
form.name.data = '' | |||||
return redirect(url_for('.index')) | |||||
return render_template('index.html', | |||||
form=form, name=session.get('name'), | |||||
known=session.get('known', False)) | |||||
return render_template('index.html') |
@ -1,16 +1,9 @@ | |||||
{% extends "base.html" %} | {% extends "base.html" %} | ||||
{% import "bootstrap/wtf.html" as wtf %} | |||||
{% block title %}Flasky{% endblock %} | {% block title %}Flasky{% endblock %} | ||||
{% block page_content %} | {% block page_content %} | ||||
<div class="page-header"> | <div class="page-header"> | ||||
<h1>Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1> | |||||
{% if not known %} | |||||
<p>Pleased to meet you!</p> | |||||
{% else %} | |||||
<p>Happy to see you again!</p> | |||||
{% endif %} | |||||
<h1>Hello, {% if current_user.is_authenticated %}{{ current_user.username }}{% else %}Stranger{% endif %}!</h1> | |||||
</div> | </div> | ||||
{{ wtf.quick_form(form) }} | |||||
{% endblock %} | {% endblock %} |
@ -0,0 +1,30 @@ | |||||
"""empty message | |||||
Revision ID: 655013143dbb | |||||
Revises: e7148b675688 | |||||
Create Date: 2018-11-01 23:41:21.492859 | |||||
""" | |||||
from alembic import op | |||||
import sqlalchemy as sa | |||||
# revision identifiers, used by Alembic. | |||||
revision = '655013143dbb' | |||||
down_revision = 'e7148b675688' | |||||
branch_labels = None | |||||
depends_on = None | |||||
def upgrade(): | |||||
# ### commands auto generated by Alembic - please adjust! ### | |||||
op.add_column('users', sa.Column('email', sa.String(length=64), nullable=True)) | |||||
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True) | |||||
# ### end Alembic commands ### | |||||
def downgrade(): | |||||
# ### commands auto generated by Alembic - please adjust! ### | |||||
op.drop_index(op.f('ix_users_email'), table_name='users') | |||||
op.drop_column('users', 'email') | |||||
# ### end Alembic commands ### |