-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[account,auth][m]: (fixes #2) working user accounts with registration…
…, login and logout. * TODO: issue with showing user info in top nav as mako system does not support context_processor (need to switch to jinja - see #13)
- Loading branch information
1 parent
04b174d
commit 6add8e1
Showing
7 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from flask import Blueprint, request, url_for, flash, redirect | ||
from flaskext.mako import render_template | ||
from flaskext.login import login_user, logout_user | ||
from flaskext.wtf import Form, TextField, PasswordField, validators | ||
|
||
import bibserver.dao as dao | ||
|
||
blueprint = Blueprint('account', __name__) | ||
|
||
|
||
@blueprint.route('/') | ||
def index(): | ||
return 'Accounts' | ||
|
||
|
||
class LoginForm(Form): | ||
username = TextField('Username', [validators.Required()]) | ||
password = PasswordField('Password', [validators.Required()]) | ||
|
||
@blueprint.route('/login', methods=['GET', 'POST']) | ||
def login(): | ||
form = LoginForm(request.form, csrf_enabled=False) | ||
if request.method == 'POST' and form.validate(): | ||
password = form.password.data | ||
username = form.username.data | ||
user = dao.Account.get(username) | ||
if user and user.check_password(password): | ||
login_user(user, remember=True) | ||
flash('Welcome back', 'success') | ||
return redirect(url_for('home')) | ||
else: | ||
flash('Incorrect email/password', 'error') | ||
if request.method == 'POST' and not form.validate(): | ||
flash('Invalid form', 'error') | ||
return render_template('account/login.html', form=form) | ||
|
||
|
||
@blueprint.route('/logout') | ||
def logout(): | ||
logout_user() | ||
flash('You are now logged out', 'success') | ||
return redirect(url_for('home')) | ||
|
||
|
||
class RegisterForm(Form): | ||
username = TextField('Username', [validators.Length(min=3, max=25)]) | ||
email = TextField('Email Address', [validators.Length(min=3, max=35)]) | ||
password = PasswordField('New Password', [ | ||
validators.Required(), | ||
validators.EqualTo('confirm', message='Passwords must match') | ||
]) | ||
confirm = PasswordField('Repeat Password') | ||
|
||
@blueprint.route('/register', methods=['GET', 'POST']) | ||
def register(): | ||
# TODO: re-enable csrf | ||
form = RegisterForm(request.form, csrf_enabled=False) | ||
if request.method == 'POST' and form.validate(): | ||
account = dao.Account(id=form.username.data, email=form.email.data) | ||
account.set_password(form.password.data) | ||
account.save() | ||
login_user(account, remember=True) | ||
flash('Thanks for signing-up', 'success') | ||
return redirect(url_for('home')) | ||
if request.method == 'POST' and not form.validate(): | ||
flash('Please correct the errors', 'error') | ||
return render_template('account/register.html', form=form) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<%def name="render_field(field, **kwargs)"> | ||
${ field.label } | ||
<div class="input"> | ||
${ field(**kwargs) } | ||
% if field.errors: | ||
<ul class="errors"> | ||
% for error in field.errors: | ||
<li>${ error }</li> | ||
% endfor | ||
</ul> | ||
% endif | ||
</div> | ||
</%def> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<%include file="/header.html"/> | ||
<%namespace name="helpers" file="/_formhelpers.html" /> | ||
|
||
<form method="post" action=""> | ||
${ helpers.render_field(form.username, placeholder="your username") } | ||
${ helpers.render_field(form.password, placeholder="********") } | ||
<input type="submit" value="Login" class="btn" /> | ||
</form> | ||
|
||
<%include file="/footer.html"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<%include file="/header.html"/> | ||
<%namespace name="helpers" file="/_formhelpers.html" /> | ||
|
||
<form method="post" action="${url_for('account.register')}"> | ||
${ helpers.render_field(form.username, placeholder="myusername") } | ||
${ helpers.render_field(form.email, placeholder="hello@mywebsite.org") } | ||
${ helpers.render_field(form.password, placeholder="********") } | ||
${ helpers.render_field(form.confirm, placeholder="********") } | ||
<input type="submit" value="Signup" class="btn" /> | ||
</form> |