Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getRedirectResult always returns null even though user has authenticated successfully with signInWithRedirect #8652

Open
hoppybunny opened this issue Nov 23, 2024 · 1 comment

Comments

@hoppybunny
Copy link

hoppybunny commented Nov 23, 2024

Operating System

macOS

Environment (if applicable)

React Native

Firebase SDK Version

11.0.2

Firebase SDK Product(s)

Auth

Project Tooling

React Native with Expo, currently running on the web

Detailed Problem Description

My app is built using React Native Expo framework, currently running on the web. I am implementing Google sign-in using Firebase Authentication with signInWithRedirect(). After a successful sign-in, getRedirectResult() keeps returning null instead of the expected result, even though the user is authenticated and signed in successfully.

I followed the best practices here: https://firebase.google.com/docs/auth/web/redirect-best-practices

I am currently using Option 1 as I am hosting the app on firebase, and having a custom domain.
This is what I have already done:

  • I m using a custom domain and I've set the firebase config authDomain to my custom domain
  • I've added the custom domain in the Authorized redirect URIs in GCP console
  • I've also added my custom domain as a authorized domain in firebase console
  • I've also allowed for all third party cookies.

Why is this still happening? I tried various suggestions on Stack Overflow and GitHub issues but none worked. I suspect that because signInWithRedirect forces a full page reload, the result returns as null.

Please help! Thank you

Steps and code to reproduce issue

2 ways of writing the code that I tried:

Method 1

  const handleGoogleSignIn = async () => {
    const provider = new GoogleAuthProvider();
    try {
      await signInWithRedirect(auth, provider)
      } catch (error) {
      console.error(error);
      }
  }

  // This function handles the result after the user is redirected back
const handleRedirectSignIn = async () => {
  try {
    const result = await getRedirectResult(auth); 
    alert(result) //this returns null
    if (result && result.user) {
      alert(`Welcome!`);
      router.push('/HomeScreen'); 
    } else {
      alert('User not authenticated.');
    }
  } catch (error) {
    console.error("Error during redirect sign-in:", error);
    alert('Failed to sign in with Google.');
  }
};

useEffect(() => {
  handleRedirectSignIn();  
}, []); 

Method 2:

    const provider = new GoogleAuthProvider();
    try {
      await signInWithRedirect(auth, provider)
      const result = await getRedirectResult(auth);
      if (result) {
        alert('Sign-in successful!');
        router.push('/HomeScreen');
      } else {
        alert('Failed to sign in with Google.');
      } 
@hoppybunny hoppybunny added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Nov 23, 2024
@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@jbalidiong jbalidiong added api: auth needs-attention and removed needs-triage new A new issue that hasn't be categoirzed as question, bug or feature request labels Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants