Skip to main content

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.

Project details


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.2.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

Sanic_Auth-0.2.0-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file Sanic-Auth-0.2.0.tar.gz.

File metadata

  • Download URL: Sanic-Auth-0.2.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Sanic-Auth-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b7cb9e93296c035ada0aa1ebfb33f9f7b62f7774c519e374b7fe703ff73589cb
MD5 4b8991333fc8bf3d682dc14884333e7d
BLAKE2b-256 6e22dc4a331ddb3fd5137e6ac4d0a7d9eeccbf9918f4b41da08c6574051fbe29

See more details on using hashes here.

File details

Details for the file Sanic_Auth-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for Sanic_Auth-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6431c2dc06995f2da5ecf0c2fc23208bb0afd5ae0048c060537a79e0bb2a255f
MD5 54cbef82c45485616344574d461e966c
BLAKE2b-256 862a831d18347b7418d3125fea4fb58e28cc3296ff19cae3dd7b93801a80dd50

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page