Skip to main content

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

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-firebase-custom-auth-0.1.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

django_firebase_custom_auth-0.1-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

Supported by

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