What's the best flow for authentication using Firebase in NextJS and ExpressJS system

Viewed 107

I'm currently developing a front-end system (build using NextJS) and backend (build using Node/ExpressJS). I'm using Firebase for the authentication system. Users can log in / signup to the website using email/password, Google or Facebook login. The system also needs to store user data on its own, and it uses the Postgres database for its persistence storage.

Currently, this is what I have:

For Registration

  • with username and password

    • user fill in the sign-up form
    • frontend call the firebase.auth().createUserWithEmailAndPassword(email, password)
    • frontend call the /register endpoint on the backend with the Authorization header consists of the token that's generated from the above step
  • with google / login

    • user click on the sign up with Google / Facebook button
    • frontend call the firebase method to open up a popup for Google / Facebook login
    • frontend call the /register endpoint on the backend with the Authorization header consists of the token that's generated from the previous step.

For Login

  • with username/password
    • frontend call the firebase.auth().signInWithEmailAndPassword(email, password) and store the token generated from the response in cookie
    • all of the subsequent requests to the backend will use the token as an Authorization header

The only thing that I'm still not quite sure about is Google / Facebook because it uses the same firebase function. How to differentiate between login/registration for the social media login apart? And is there a better way for the system to ensure that the user data is created in the database? At the moment, when the /register endpoint throws an error, I'll call the firebase.auth().deleteUser function afterwards.

I believe this must be a common scenario for a website that uses Firebase authentication. I really appreciate any suggestions.

0 Answers
Related