Search code examples
javascriptfirebasefirebase-authenticationfirebaseui

Once completing a Firebase Authentication, how can I post data to the signInSuccessUrl?


I have:

const uiConfig = {
  // Popup signin flow rather than redirect flow.
  signInFlow: 'popup',
  credentialHelper: 'none',
  // Redirect to /signedIn after sign in is successful. Alternatively you can provide a callbacks.signInSuccess function.
  signInSuccessUrl: '/app',
  // We will display Google and Facebook as auth providers.
  signInOptions: [
    { provider: firebase?.auth?.EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD }
  ]
};
...
<StyledFirebaseAuth uiConfig={uiConfig} firebaseAuth={firebase.auth()} />

But when the user authenticates, how can I tell if it's a login or a first time creation?


Solution

  • Use the signInSuccessWithAuthResult callback. You can check if the user is a new user with authResult.additionalUserInfo.isNewUser:

    const uiConfig = {
      signInFlow: 'popup',
      credentialHelper: 'none',
      signInSuccessUrl: '/app',
      signInOptions: [{ 
        provider: firebase?.auth?.EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD
      }],
      callbacks: {
        signInSuccessWithAuthResult: function(authResult, redirectUrl) {
          const isNewUser = authResult.additionalUserInfo.isNewUser;
    
          // Do something with the returned AuthResult.
          // Return type determines whether we continue the redirect automatically
          // or whether we leave that to developer to handle.
          return true;
        }
      }
    };
    ...
    <StyledFirebaseAuth uiConfig={uiConfig} firebaseAuth={firebase.auth()} />