Django Middleware to authenticate users with Appwrite
Project description
Appwrite Middleware for Django
A Django middleware to authenticate users with Appwrite.
Installation
To install django_appwrite
, simply run:
$ pip install django-appwrite
Usage
- Add
django_appwrite
to your INSTALLED_APPS list in your Django project'ssettings.py
file:
INSTALLED_APPS = [
...,
'django_appwrite',
...
]
- Add
django_appwrite.middleware.AppwriteMiddleware
to your MIDDLEWARE list in your Django project'ssettings.py
file. It should be placed after theAuthenticationMiddleware
:
MIDDLEWARE = [
...,
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_appwrite.middleware.AppwriteMiddleware',
...
]
- Configure the Appwrite client settings in your Django settings file:
APPWRITE = {
'PROJECT_ENDPOINT': 'https://example.com/v1',
'PROJECT_ID': 'PROJECT_ID',
'PROJECT_API_KEY': 'PROJECT_API_KEY',
}
Setting | Default | Description |
---|---|---|
PROJECT_ENDPOINT |
The endpoint of your Appwrite project. You can find this in the Appwrite console under Settings > General. | |
PROJECT_ID |
The ID of your Appwrite project. You can find this in the Appwrite console under Settings > General. | |
PROJECT_API_KEY |
The API key of your Appwrite project. You can find this in the Appwrite console under Settings > API Keys. | |
AUTH_HEADER |
HTTP_AUTHORIZATION | The header name that will be used to get the JWT from the authorization header. The value of this header should be Bearer [JWT] |
VERIFY_EMAIL |
False | If set to True , the middleware will check if the user's email address has been verified in Appwrite before authenticating the user. If the email address has not been verified, the user will not be authenticated. |
VERIFY_PHONE |
False | If set to True , the middleware will check if the user's phone number has been verified in Appwrite before authenticating the user. If the phone number has not been verified, the user will not be authenticated. |
PREFIX_EMAIL |
The prefix to use for the email address when checking if the user's email address has been verified. This is useful if you are integrating django_appwrite to existing projects that already have users. |
How it works
This middleware class will authorize the user by checking the JWT in the Authorization
header. The JWT is obtained from the Authorization
header and is then sent to the Appwrite API to verify the JWT. If the JWT is valid, the user will be authenticated.
Please note that this middleware is intended to be used in conjunction with the Appwrite client-side SDK to authorize users on the frontend, and does not provide any APIs for user authentication on its own.
Example
This is an example of how you can configure your frontend to use this middleware. Note that this example uses the axios library.
// axios interceptor to add jwt to the request header
import axios from 'axios';
import { Client, Account } from 'appwrite';
const client = new Client()
.setEndpoint('https://example.com/v1')
.setProject('PROJECT_ID')
.setKey('PROJECT_API_KEY');
const account = new Account(client);
axios.interceptors.request.use(async (config) => {
// get jwt from your auth service/provider
const { jwt } = await yourAuthService();
config.headers = {
...config.headers,
Authorization: `Bearer ${jwt}`
}
return config;
});
License
The django-appwrite package is released under the MIT License. See the bundled LICENSE file for details.
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.