This repository has been archived by the owner on Feb 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
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,23 @@ | ||
import functools | ||
from flask_login import current_user, LoginManager | ||
from flaskapp.database import Credential | ||
|
||
login_manager = LoginManager() | ||
|
||
|
||
def admin_required(func): | ||
@functools.wraps(func) | ||
def _admin_required(*args, **kw): | ||
admin = current_user.is_authenticated and current_user.is_admin | ||
if not admin: | ||
return login_manager.unauthorized() | ||
return func(*args, **kw) | ||
return _admin_required | ||
|
||
|
||
@login_manager.user_loader | ||
def load_user(user_id): | ||
user = Credential.query.get(user_id) | ||
if user is not None: | ||
user._authenticated = True | ||
return user |
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,44 @@ | ||
from flask_sqlalchemy import SQLAlchemy | ||
from werkzeug.security import generate_password_hash, check_password_hash | ||
|
||
db = SQLAlchemy() | ||
|
||
|
||
class Credential(db.Model): | ||
__tablename__ = 'credential' | ||
id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
email = db.Column(db.Unicode(128), nullable=False) | ||
password = db.Column(db.Unicode(128), nullable=False) | ||
is_active = db.Column(db.Boolean, default=True) | ||
is_admin = db.Column(db.Boolean, default=False) | ||
|
||
is_anonymous = False | ||
|
||
def __init__(self, *args, **kw): | ||
super(Credential, self).__init__(*args, **kw) | ||
self._authenticated = False | ||
|
||
def set_password(self, password): | ||
self.password = generate_password_hash(password) | ||
|
||
@property | ||
def is_authenticated(self): | ||
return self._authenticated | ||
|
||
def get_id(self): | ||
return self.id | ||
|
||
|
||
class User(db.Model): | ||
__tablename__ = 'user' | ||
id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
email = db.Column(db.Unicode(128), nullable=False) | ||
firstname = db.Column(db.Unicode(128)) | ||
lastname = db.Column(db.Unicode(128)) | ||
strava_token = db.Column(db.String(128)) | ||
age = db.Column(db.Integer) | ||
weight = db.Column(db.Numeric(4, 1)) | ||
max_hr = db.Column(db.Integer) | ||
rest_hr = db.Column(db.Integer) | ||
vo2max = db.Column(db.Numeric(4, 2)) | ||
|
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 @@ | ||
from flask_wtf import FlaskForm | ||
import wtforms as f | ||
from wtforms.validators import DataRequired, Email | ||
|
||
|
||
class LoginForm(FlaskForm): | ||
email = f.StringField('Email', validators=[DataRequired("Insert a valid email."), Email()]) | ||
password = f.PasswordField('Password', validators=[DataRequired("Insert your password")]) | ||
|
||
display = ['email', 'password'] |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from flaskapp.views.home import home | ||
from flaskapp.views.auth import auth | ||
|
||
blueprints = [home] | ||
blueprints = [home, auth] |
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,36 @@ | ||
from flask import Blueprint, render_template, redirect, flash, make_response | ||
from flask_login import current_user, login_user, logout_user, login_required | ||
from flaskapp.database import db, Credential | ||
from flaskapp.forms import LoginForm | ||
from flaskapp.views.home import index | ||
from werkzeug.security import check_password_hash | ||
|
||
auth = Blueprint('auth', __name__) | ||
|
||
|
||
@auth.route('/login', methods=['GET', 'POST']) | ||
def login(): | ||
|
||
if hasattr(current_user, 'is_authenticated') and current_user.is_authenticated is True: | ||
return make_response(index(), 403) | ||
|
||
form = LoginForm() | ||
if form.validate_on_submit(): | ||
email, password = form.data['email'], form.data['password'] | ||
|
||
credential = db.session.query(Credential).filter(Credential.email == email).first() | ||
if credential is not None and check_password_hash(credential.password, password): | ||
login_user(credential) | ||
# TODO: get to data-service for retrieve User data and fill User table | ||
return redirect('/') | ||
else: | ||
flash('Wrong email or password', category='error') | ||
return make_response(render_template('login.html', form=form), 401) | ||
return render_template('login.html', form=form) | ||
|
||
|
||
@auth.route("/logout") | ||
@login_required # throws 401 HTTPException if user is anonymous | ||
def logout(): | ||
logout_user() | ||
return redirect('/') |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
flask | ||
wtforms | ||
werkzeug |