Skip to main content

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.

Metadata

Release files for authlier-flask 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for authlier-flask 1.0.1
File Size Uploaded
authlier_flask-1.0.1.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for authlier-flask 1.0.1
File Interpreter ABI Platform
authlier_flask-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / authlier_flask-1.0.1.tar.gz

Download URL authlier_flask-1.0.1.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
73a5967c7a5507408349d29d0beb1b774c64d103480c457cc7094af08edf3dc3
BLAKE2b-256 checksum
How to use checksums
e01fe4f7a6e400f099c23686f602e6b2f49eacc853a9dda8d04c79ee5ba5691f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release files / authlier_flask-1.0.1-py3-none-any.whl

Download URL authlier_flask-1.0.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e97528a10298153d31c2974f866ea0ec668e4feebe83850981b232c55292958d
BLAKE2b-256 checksum
How to use checksums
19a72819fd8baf6b163cacad9ad60f54196f7d982232593a663a1ca66a106d89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page