Skip to content

Commit

Permalink
udpate with flask_login for login management
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoWzk committed Feb 17, 2019
1 parent 110be76 commit 5d7b539
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 89 deletions.
174 changes: 91 additions & 83 deletions .idea/workspace.xml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_login import LoginManager, LOGIN_MESSAGE_CATEGORY, LOGIN_MESSAGE
from config import config

moment = Moment()
db = SQLAlchemy()
bootstrap = Bootstrap()
mail = Mail()
login_manager = LoginManager()


def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
LOGIN_MESSAGE_CATEGORY = 'info'
LOGIN_MESSAGE = 'This page needs authentications'

moment.init_app(app)
db.init_app(app)
bootstrap.init_app(app)
mail.init_app(app)
login_manager.init_app(app)

from .main import main as main_blueprint
app.register_blueprint(main_blueprint)

from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint, url_prefix='/auth')

return app
Binary file modified app/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file modified app/__pycache__/models.cpython-35.pyc
Binary file not shown.
Binary file modified app/auth/__pycache__/views.cpython-35.pyc
Binary file not shown.
16 changes: 15 additions & 1 deletion app/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,30 @@
from flask_login import login_required, login_user, logout_user
from ..auth import auth

@auth.route('/login')

@auth.route('/login', methods=['GET', 'POST'])
def sign_in():
form = loginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and user.verify_pass(form.password.data):
login_user(user)
return redirect(url_for('auth.dashboard'))
else:
flash('Invalid credentials')
return render_template('auth/login.html', form=form)
return render_template('auth/login.html', form=form)


@auth.route('/logout', methods=['GET', 'POST'])
@login_required
def logout():
logout_user()
return redirect(url_for('main.index'))


@auth.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
return render_template('auth/dashboard.html')

Binary file modified app/main/__pycache__/views.cpython-35.pyc
Binary file not shown.
7 changes: 4 additions & 3 deletions app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

from flask import render_template, redirect, url_for, flash
from ..main import main
from app.forms import signupForm
from ..forms import signupForm
from app.models import User, db


@main.route('/')
@main.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')


@main.route('/register')
@main.route('/register', methods=['GET', 'POST'])
def user_register():
form = signupForm()
if form.validate_on_submit():
Expand All @@ -26,4 +26,5 @@ def user_register():
user = User(username=form.username.data, email=form.email.data, password=form.password.data)
db.session.add(user)
db.session.commit()
return redirect(url_for('auth.sign_in'))
return render_template('register.html', form=form)
12 changes: 10 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# encoding:utf-8
# create db models here

from . import db
from . import db,login_manager
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin


class User(db.Model):
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
username = db.Column(db.String(64), unique=True, nullable=False, index=True)
Expand All @@ -22,3 +23,10 @@ def password(self, password):

def verify_pass(self, password):
return check_password_hash(self.password_hash, password)

@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)

def __repf__(self):
return 'User {}'.format(self.username)

0 comments on commit 5d7b539

Please sign in to comment.