skip to Main Content

I have a NextJS web app where a user can login and perform the typical e-commerce tasks such as editing their emails, name, preferences, etc.

Currently a user can login using their email or Google account as outlined here: https://firebase.google.com/docs/auth/web/firebaseui

There is a step I have on the account creation after the user successfully authenticates through email or Google account, the code will look for an existing user object in the Firestore to see if an existing account already exists.

This works fine for now but I notice some slowness when a new user creates their account and I suspect the cause is this check that’s being done.

This is my first e-commerce website and I’d like to know what the optimal way to do this. I was thinking of moving this action of checking to a Next API middleware but I figure I’d ask here first if that’s worth doing.

Below is the code for the login page.

... 
export default function Login() {
  const router: NextRouter = useRouter()
  const user = useAuthContext()

  const { profile, setProfile } = useProfileContext()

  const uiConfig = {
    signInOptions: [
      GoogleAuthProvider.PROVIDER_ID,
      EmailAuthProvider.PROVIDER_ID,
    ],
    signInSuccessUrl: "/account",  # Reroute to user account dashboard 
  }

  useMemo(() => {
    if (user && !profile) {
      uidProfileExists(user.uid)
        .then((exists: boolean) => {
          if (!exists) {
            // This will create a new profile as recognized by the Firebase
            // database.  A user login profile may already exist through
            // Gmail, GitHub, or some authentication provider.
            createNewUserProfile(user)
          }
        })
        .then(() =>
          getUserProfile(user.uid).then((profileData) => setProfile(profileData))
        )
    }
  }, [])
... 

2

Answers


  1. You don’t need to check if new user’s UID exists because they’re unique per user… if you’re using Firebase Auth to handle authentication and user creation of course.

    For every new user created (even if it’s created by google, github, facebook… authentication) you’ll get a firebase auth UID that you can use as user’s doc Id then if you need, after user’s login just check if user’s UID doc already exists witch is a really fast task.

    You also doesn’t need to check if user email/cellphone exists when creating/editing user’s account but you need to handle errors while doing this because if the email is already taken, Firebase auth will returns error

    Login or Signup to reply.
  2. If you want to use google auth on backend there is no func for that.
    But There is ability to use user name password registration and login. With that you can create custom (your own) login page and send data to your backend server which will send data toward rest api to create new user.

    But if you want to make login using google the only way is to use client side library for that and it takes so much time as it takes. That will open google website popup or redirect you to google federated identity page (or popup) and then you get auth object with authenticated user inside.

    After login user auth object (with uuid) is stored in your browser as well as nextjs session so having those two depending of your needs you can create and manage user related profile data inside your backend database or firestore backend.

    Both email and password flow as well as google flow give you uuid of user that then could be bounded to request to backend server or firebase backend.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search