1
votes

I have a react app for frontend and node for backend and mongodb for database. I want to make the users signup and login through their mobile numbers. In firebase there's an authentication method that make users to login through OTP. What I want to achieve is, whenever the users create an account with their mobile number through firebase, it should save in my mongodb database and when they try to login the number should be checked from mongodb database and the user must be logged in. how can i achieve this?

Summary : Frontend - React Middleware - Firebase(only for OTP) Backend - Nodejs Database - MongoDB

Thanks in Advance

1

1 Answers

0
votes

That sounds doable. When Firebase authentication is complete, you will have user information from firebase, and from there you would need to implement the mongo part yourself.

Here are the docs for implementing phone-based authentication via the Firebase SDK: https://firebase.google.com/docs/auth/web/phone-auth (Those docs also link to https://opensource.google/projects/firebaseui, which looks like a library of ready-made UI widgets for firebase). While there are a number of concerns they cover (e.g. CAPTCHA), essentially:

  1. you ask the user for their phone #, make one call to the Firebase SDK to send an SMS
  2. you ask the user for the code that was sent
  3. When they enter it, you make a second call to the Firebase SDK to verify the code
  4. When that operation succeeds it means Firebase has verified their identity, and you will be able to access a user object via the Firebase SDK that represents the logged-in user.

At this point you would implement storage of the user information you want into your own database -- how you do that is totally up to you. I would recommend using the user id from firebase auth as the user id/unique key in your own db.

As an example, one approach would be to send the firebase auth user id, along with any other user info you are storing, to your own node app, e.g. at a /user/create endpoint, and have your node app store it in mongo. At log in, you would use Firebase again to verify the user's identity, and once you have the user id, you would need something like a /user/<userid> endpoint in your node app, to look up an existing user by id. Your app will use mongo as the source of truth for all your user data, and you'll use firebase only to authenticate and get their id.