Django backend for Firebase custom authentication.
Project description
Django Firebase Custom Auth
Django backend for Firebase custom authentication.
Requirements
- Django 3
- Python 3
Setup
Install dependencies.
pip install djangorestframework firebase-admin
Install this package.
pip install django-firebase-custom-auth
Add django_firebase_custom_auth
to your INSTALLED_APPS
in your settings.py
.
INSTALLED_APPS = (
...,
'rest_framework',
...,
'django_firebase_custom_auth',
)
Add URL patterns.
urlpatterns = [
path('custom-auth/', include('django_firebase_custom_auth.urls')),
]
Specify a file path service account info is stored.
SERVICE_ACCOUNT_KEY_FILE_PATH = os.path.join(BASE_DIR, 'serviceAccountKey.json')
(Optional) Provide a key to generate a custom token. The default value is id
.
Make sure this value is a primary key your user model has.
CUSTOM_TOKEN_KEY = 'uuid'
Now you can send a user's credentials (username and password) from your Firebase client app to Django backend. And your server will return a custom token if the credentials are valid. And you can sign in with Firebase using the token. Below is an example in JavaScript.
const url = "http://127.0.0.1:8000/custom-auth/login/"
const credentials = {
username: username,
password: password,
}
axios.post(url, credentials).then((resp) => {
const token = resp.data.token
firebase.auth().signInWithCustomToken(token)
.then(() => {
// Sign in with Firebase
})
.catch((err) => {
// Handle an error
})
})
Use Case
Let's say your users will be added by admin manually and you want to authenticate users without email. Since Firebase built-in authentication requires email and password, you need a server that provides a custom token to authenticate users with Firebase. This package can handle the custom authentication. For more details, see these resources below.
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.
Source Distribution
Built Distribution
Hashes for django-firebase-custom-auth-0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba3ca2f73cd750f256e1f524dc5b2d7c66ff5dffd27db111b8fab427b0b1f517 |
|
MD5 | c3c84fd2d85b3fe22d2ebc7f8c623e6d |
|
BLAKE2b-256 | 42893f3b8c7e05a93c16d2cb2403f57b8db3405a332b48ed2165cddd5d8959a4 |
Hashes for django_firebase_custom_auth-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9a0e09524f779b13e603e05bad301d86b5e4e8c647af92dffa2a48d6dd43e4f |
|
MD5 | 42014abdb533df04f8a0c096ad1351c6 |
|
BLAKE2b-256 | 180b4580b10c8db068ccf2692a7df4dc4e2be781e19a9cdded3f98d6638ba791 |