Firebase 9 - How to detect first time login of new user using googleAuth


In this short article, we will see how we can detect the first-time login of a new user using googleAuth in firebase 9.

If you are working with google Authentication in firebase, then to detect if the user has signed-in for the first time or not, you have to use the AdditionalUserInfo that is provided by firebase.

The AdditionalUserInfo contains additional user information as a result of a successful sign-in or re-authentication operation.

Check if the user logged in for the first time using googleAuth firebase.

Let's see the steps on how to import the getAdditionalUserInfo in our firebase project to get additional user information.

In firebase 9, we have to import the getAdditionalUserInfo module from firebase/auth.

import { getAdditionalUserInfo } from "firebase/auth";

Next, after a successful sign-in with the signInWithPopup we can use the isNewUser property provided by getAdditionalUserInfo module to check if the user has signed in for the first time or not.

import {  signInWithPopup, getAdditionalUserInfo } from "firebase/auth";

function signInWithGoogleAuth() {
    signInWithPopup(auth, provider)
        .then(async (result) => {
            const isFirstLogin = getAdditionalUserInfo(result).isNewUser
         }
}

isNewUser returns boolean as result.

So in the above code, if the user have signed in for the first time, isFirstLogin will return true, else it will return false.

This is how you can check if user is authenticated for the first time in Firebase Google Authentication.


💾 Recommended Articles