How to implement Firebase custom authentication backend?
Asked Answered
M

3

6

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

Maragaret answered 3/9, 2017 at 12:40 Comment(2)
See the list of use-cases in the Cloud Functions for Firebase documentation for examples of custom authentication providers for LinkedIn and Instagram.Jarvey
Can I simply write a function in 'firebase functions' that will read the uid of the user whose username (uid) was passed in, then mint a custom token after doing a lookup in a table within firebase database itself that associates with the uid of the master user, then send back that uid's custom minted token (master's uid is different from the passed in uid) so that the client can now login with that custom token?Maragaret
G
4

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

Gibbs answered 7/5, 2022 at 8:20 Comment(0)
H
1

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

Haroun answered 3/9, 2017 at 12:54 Comment(1)
So where do I write the service that says that now subordinate user should now assume the identity of super user A? Can that be a firebase cloud function?Maragaret
C
1

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

Cockscomb answered 24/10, 2017 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.