Firebase verify email password in cloud function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Firebase verify email password in cloud function



I have a requirement to accept custom usernames into my site (Requirement from biller with strict limitations of alpha numeric.) And these usernames should be interchangeable with the user's email address for login purposes.



I allow the user to signup and login with their email and password through the standard firebase email password authentication. The user is registered at the biller, who then returns a custom generated username to the application via a postback.



I have created a usernames table that contains the UID of the user each username belongs to (Initially there is the email, and biller generated usernames)



When the user attempts to login, I go to the usernames table and lookup the UID. At this point I would like to use the UID just looked up, and the password supplied by the user to log the user in through the standard firebase authentication system.



I have been unable to find any way to verify the user's password is valid against a looked up user account inside of cloud functions so that I can generate a custom token.



I could lookup the user by the username, find the email, send it back to the client and allow the login to take place with that email and the password the user supplied, but would prefer to avoid it as that will allow usernames and email addresses to be associated with each other by bad actors.





I'm not sure I understand. Are you trying to build username+password authentication? If so, have a look at this sample: github.com/firebase/functions-samples/tree/Node-8/…
– Frank van Puffelen
Aug 8 at 17:41





@FrankvanPuffelen I am trying to add the ability to associate a username (non email) with the existing authentication system.
– major-mann
Aug 8 at 17:44





Effectively (From the example you gave) github.com/firebase/functions-samples/blob/Node-8/… would need to verify against the internal authentication system (Using the email looked up off the user record.) I need to verify that the caller of the function should receive a token back
– major-mann
Aug 8 at 17:48





Associating a name with a Firebase Authentication profile has been covered quite a few times here already, e.g. stackoverflow.com/questions/31038611/…. I assume your approach is different, but have a hard time parsing it down to code in my mind. Can you show the minimal code that reproduces where you got stuck.
– Frank van Puffelen
Aug 8 at 19:28






I think you have misunderstood what I need.I will try to edit the question some more now. I need the user to be able to sign in with a username (Separate from the email) and password. I can lookup a user based on the username, but I have no way to verify that their password is correct
– major-mann
Aug 8 at 19:32




1 Answer
1



In your cloud function you can install and use the firebase package besides firebase-admin, and initialize it as if you were initializing a web page. That way you can use the admin SDK to find the email of the username, and then use firebase from the cloud function to authenticate, using signInWithEmailAndPassword. If it succeeds you can then generate a custom token and send it to the client.


firebase


firebase-admin


firebase


signInWithEmailAndPassword



I don't know if this is the best approach but it works.





This sounds workable... off to try it... Thank you so much!
– major-mann
Aug 8 at 21:09





Is there any chance you could offer some guidance on how you installed / initialized firebase in this circumstance? I'm struggling to be able to get the auth client initialized
– major-mann
Aug 9 at 14:32





Thanks! Got it going :)
– major-mann
Aug 9 at 17:59





Sorry, only got to see your message now, glad it worked! =)
– Ricardo Smania
Aug 9 at 19:59





np.. .wish I can upvote it more :D
– major-mann
Aug 10 at 8:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard