How to implement Firebase custom authentication backend?

9.6k views Asked by At

I want custom firebase authentication where a user manages the roles of subordinate users. I need guidance on understanding on how to implement my own backend authentication system. Everywhere the documentation keeps mentioning that 'send the username and password to your backend that will generate a custom token'. What is this backend? where do I pursue this? My knowledge domain is firebase, firebase functions, angular 2/4, ionic2 for this discussion... thanks

3

There are 3 answers

1
Umar Hussain On

You will have to send the email password to the firebase sdk in using javascript in web then when the sdk success functions tell that the user has been authenticated the web page will send result to your backend server (can be nodejs or php etc) from there you have to manage your own database to handle all the role base access.

Firebase is basically authenticating the user for you and telling you that you can identify this user using the following userid and then build your own system.

Firebase has access rules but those you have to define first you cannot fully customize them for each user.

For password auth see this: https://firebase.google.com/docs/auth/web/password-auth

0
nachbar On

To use custom authentication, you need to create a JSON Web Token (JWT) on your existing backend server, after you have used your existing backend server to validate the username and password of the user (or however else your backend server validates your users).

To create that JWT, use the configuration described at https://firebase.google.com/docs/auth/admin/create-custom-tokens?authuser=0#create_custom_tokens_using_a_third-party_jwt_library

There is PHP and Ruby code available at that page, for anyone using a language that does not have an SDK available from Google, but which does have a JWT library available.

The JWT is signed with your private key, which you can obtain as indicated at https://firebase.google.com/docs/auth/admin/create-custom-tokens?authuser=0#create_custom_tokens_using_a_third-party_jwt_library

Although that page describes initializing the SDK, this section also has instructions for creating the private key for your service account using the Firebase console at https://console.firebase.google.com/u/0/project/_/settings/serviceaccounts/adminsdk

0
shusson On

An easy way to do custom auth with Firebase is using an external identity provider. Auth0 is an example of such a provider.

Guide:

https://shusson.info/post/using-firebase-and-auth0-together

code:

https://github.com/shusson/firebase-custom-auth