Skip to main content

Authlier flask plugin

Project description

Authlier-Flask

Overview

Authlier-Flask is a lightweight Flask extension that integrates Authlier with your application, providing seamless user authentication and session management. This plugin allows you to use Authlier's token-based authentication system within the context of a Flask app.

Features:

  • User Authentication: Use Authlier for username/password verification.
  • Session Management: Maintain user sessions using cookies or tokens.
  • Token-Based Access Control: Protect routes based on authenticated users.
  • Customizable User Identifiers and Metadata Handling.

Installation

To install the authlier-flask package, run:

pip install authlier-flask

Alternatively, if you're setting up your project from scratch or using a virtual environment with requirements files, add it to your requirements.txt.

Example requirements.txt

flask>2.0
authlier-flask

Usage

  1. Initialize Authlier Manager in Your Flask App:

    Create a new instance of AuthlierManager and pass it to each route where you need authentication. Store API_KEY and SECRET_KEY somewhere safe (like environment variables!)

    from flask import Flask, redirect, url_for
    from authlier import AuthlierManager, authlier_required
    
    app = Flask(__name__)
    
    # Initialize the Authlier Manager with your API key and secret.
    authlier_manager = AuthlierManager(API_KEY, SECRET_KEY)
    
    @app.route('/')
    def index():
        return 'Welcome to the Home Page!'
    
    # Protect a route with Authlier
    @authlier_required()
    def protected_route():
        return f'Hello, {current_user.username}! <a href="/logout">Logout</a>'
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  2. Register and Login Users:

    Use the register function to create new users in your system.

    from authlier import register
    
    # Create a user with username, email, phone number.
    def on_new_user():
       return 'beiller', 'beiller@gmail.com', '+1234567890'
    
    registered_user = create_user(*on_new_user())
    print(registered_user)  # Output: {'id': 1, 'username': 'beiller', ...}
    
    # Login a user with username and password.
    def on_login():
       return "user1", "password"
    
    login_response = login(*on_login())  
    
  3. Customize User Lookup Loader:

    Override the default user_lookup_loader to customize how users are identified.

    @authlier.user_identity_loader
    def user_id(user):
        return str(getattr(user, "id", None))
    
    # Define a custom function for looking up users by metadata.
    @authlier.user_lookup_loader
    def lookup_user(metadata: str):
        return {1: {'id': 1}, 2: {'id': 2}}[int(metadata)]
    
  4. Logout Users and Handle Sessions:

    Use the logout function to end a user's session.

    from authlier import logout
    
    # Log out the current user.
    def on_logout():
        return "some_token"
    
    success = logout(*on_logout())
    

Contributing

Contributions are welcome! If you find any issues or have suggestions, feel free to open a pull request.

If you'd like to contribute code, please fork the repository and create new branches for your work. When opening an issue, provide as much detail as possible so that others can help quickly understand and resolve it.

License

This project is licensed under MIT.

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

authlier_flask-1.0.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

authlier_flask-1.0.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file authlier_flask-1.0.1.tar.gz.

File metadata

  • Download URL: authlier_flask-1.0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.15

File hashes

Hashes for authlier_flask-1.0.1.tar.gz
Algorithm Hash digest
SHA256 73a5967c7a5507408349d29d0beb1b774c64d103480c457cc7094af08edf3dc3
MD5 507440823a887a0c74981cca46d5a4a9
BLAKE2b-256 e01fe4f7a6e400f099c23686f602e6b2f49eacc853a9dda8d04c79ee5ba5691f

See more details on using hashes here.

File details

Details for the file authlier_flask-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: authlier_flask-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.15

File hashes

Hashes for authlier_flask-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e97528a10298153d31c2974f866ea0ec668e4feebe83850981b232c55292958d
MD5 1b0854f965613639a135591d094e5d4d
BLAKE2b-256 19a72819fd8baf6b163cacad9ad60f54196f7d982232593a663a1ca66a106d89

See more details on using hashes here.

Supported by

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