Sanic-Auth - Simple Authentication for Sanic
Switch branches/tags
Nothing to show
Clone or download
Latest commit 5ea5eaa Apr 26, 2018


Sanic-Auth - Simple Authentication for Sanic

Sanic-Auth implements a minimal backend agnostic session-based user authentication mechanism for Sanic.

Quick Start


pip install --upgrade Sanic-Auth

How to use it

from sanic_auth import Auth
from sanic import Sanic, response

app = Sanic(__name__)
app.config.AUTH_LOGIN_ENDPOINT = 'login'

async def add_session_to_request(request):
    # setup session

auth = Auth(app)

@app.route('/login', methods=['GET', 'POST'])
async def login(request):
    message = ''
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        # fetch user from database
        user = some_datastore.get(name=username)
        if user and user.check_password(password):
            auth.login_user(request, user)
            return response.redirect('/profile')
    return response.html(HTML_LOGIN_FORM)

async def logout(request):
    return response.redirect('/login')

async def profile(request, user):
    return response.json({'user': user})

For more details, please see documentation.


BSD New, see LICENSE for details.