Sanic-Auth - Simple Authentication for Sanic
Project description
Sanic-Auth implements a minimal backend agnostic session-based user authentication mechanism for Sanic.
Quick Start
Installation
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'
@app.middleware('request')
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)
@app.route('/logout')
@auth.login_required
async def logout(request):
auth.logout_user(request)
return response.redirect('/login')
@app.route('/profile')
@auth.login_required(user_keyword='user')
async def profile(request, user):
return response.json({'user': user})
For more details, please see documentation.
License
BSD New, see LICENSE for details.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Sanic-Auth-0.3.0.tar.gz
(11.7 kB
view hashes)
Built Distribution
Close
Hashes for Sanic_Auth-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f7783b86aee36c95faea44f47e43455a61bb1609248e8ed75df62266aa70c18 |
|
MD5 | e291c351e60686c8caf07bfa736f089d |
|
BLAKE2b-256 | 5e74d15c158c18ac0a24f2c6a0ef7387b27b39de374fe2235b51e4f5ba2c0457 |