Last active
June 23, 2022 09:28
-
-
Save phndiaye/5522479 to your computer and use it in GitHub Desktop.
How to setup an authentification system through Flask and the Flask-Login extension. The database backend is a MySQL one and the ORM is SQLAlchemy (Flask-SQLAlchemy extension). It only check if a user exists and if the given password is the good one. No credentials verification.
This file contains hidden or 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
from application import app | |
# Configuration setup | |
from kuchiyose import config | |
app.config.from_object(config.DevelopmentConfig) | |
# SQL Alchemy setup | |
from flask.ext.sqlalchemy import SQLAlchemy | |
db = SQLAlchemy(app) | |
# Flask-Login setup | |
from flask.ext.login import LoginManager | |
login_manager = LoginManager() | |
login_manager.login_view = "login" | |
login_manager.session_protection = "strong" | |
login_manager.setup_app(app) | |
import views |
This file contains hidden or 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
from Foo import app, login_manager | |
from models import User | |
from forms import LoginForm | |
from flask import (render_template, redirect, | |
url_for, request, | |
g) | |
from flask.ext.login import (login_user, logout_user, | |
current_user, login_required) | |
@login_manager.user_loader | |
def load_user(userid): | |
"""Flask-Login user_loader callback. | |
The user_loader function asks this function to get a User Object or return | |
None based on the userid. | |
The userid was stored in the session environment by Flask-Login. | |
user_loader stores the returned User object in current_user during every | |
flask request. | |
""" | |
return User.query.get(int(userid)) | |
@app.before_request | |
def before_request(): | |
"""Will be executed before each request.""" | |
g.user = current_user | |
@app.route('/') | |
def blog_index(): | |
return "awesome index" | |
@app.route('/admin/') | |
@login_required | |
def admin_root(): | |
return "awesome admin" | |
@app.route('/admin/login/', methods=['GET','POST']) | |
def login(): | |
"""Logs a user in. | |
We try to catch a user with the given username. If a user exists, | |
he's logged in, else, we return a login error. | |
""" | |
if g.user is not None and g.user.is_authenticated(): | |
return redirect(url_for('admin_root')) | |
form = LoginForm() | |
if request.method == "POST" and form.validate_on_submit(): | |
username = form.username.data | |
password = form.password.data | |
remember_me = form.remember_me.data | |
try: | |
user = User.get(username) | |
except: | |
user = None | |
if user and user.check_password(password): | |
login_user(user, remember_me, force=True) | |
return redirect(request.form['next'] or url_for("admin_root")) | |
else: | |
error = 'Username/Password incorrect.' | |
return render_template('login.html.j2', | |
form=form, | |
title='Sign In', | |
error=error) | |
return render_template('login.html.j2', | |
form=form, | |
title='Sign In') | |
@app.route('/admin/logout') | |
def logout(): | |
logout_user() | |
return redirect(url_for('index')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment