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 import Sanic


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

Uploaded Source

Built Distribution

Sanic_Auth-0.1.0-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for Sanic-Auth-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ceddc78b609829574317b1a38e3d5874645f72e2093c92cdbe64b00831f0c681
MD5 92252bbbca58888ea88ee5702d8f23d5
BLAKE2b-256 cf540fceff75a19bc286e1cc63f7c2a541efad7de6695d59a59c6af59d6c7b21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for Sanic_Auth-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 474abd07f5ded2ab7e758972c1a6db4630617af9e35983210bbd594224a1c858
MD5 26c2796fc744b52b949f0bb3a2ae434f
BLAKE2b-256 5795adebd71d51c0fe6759008437305569a93730a29585e4e95be4903bd270b1

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