firebase-tools: getRedirectResult always returns null on emulator
Operating System
macOS
Browser Version
Safari 16.5.2
Firebase SDK Version
10.1.0
Firebase SDK Product:
Auth
Describe your project’s tooling
index.html with source tag.
Describe the problem
getRedirectResult always returns null on emulator. The same code works without using the emulator (on Android, that is. On MacOS Safari it doesn’t work with or without emulator, but that’s a separate issue).
Steps and code to reproduce issue
Code is very simple. It tries to get the redirect result first, which fails the first time and calls signInWithRedirect. Second time getRedirectResult is supposed to return a user credential, not null.
const app = initializeApp(firebaseConfig);
const auth = getAuth();
connectAuthEmulator(auth, "http://127.0.0.1:9099");
const provider = new GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/contacts.readonly');
provider.setCustomParameters({
'login_hint': 'user@example.com'
});
try {
console.log("before redirect");
let userCredential = await getRedirectResult(auth);
if (userCredential) {
const user = userCredential.user;
console.log("==USER==");
console.log(user);
const credential = GoogleAuthProvider.credentialFromResult(userCredential);
console.log("==CREDENTIAL==");
console.log(credential);
const token = credential.accessToken;
console.log("==TOKEN==");
console.log(token);
} else {
console.log("NO RESULT");
await signInWithRedirect(auth, provider);
}
} catch(error) {
console.error(error);
}
About this issue
- Original URL
- State: closed
- Created 10 months ago
- Comments: 16 (9 by maintainers)
I’m not completely sure if this is the cause of the issue, but can anyone try accessing their web app via “127.0.0.1:[port]” to see if it produces a different behavior?
When I tried connecting to the emulator and accessing the web app via “localhost:[port]”,
getRedirectResult()
returnsnull
. However, when I tried accessing the web app via “127.0.0.1:[port]”,getRedirectResult()
returns an instance ofUserCredential
. I’d like to confirm if this is a common factor that is causing the issue.Hey everyone, I discussed this issue with our engineering team and they mentioned that a reason for this issue to occur is that
localhost
isn’t required to map to127.0.0.1
. So when running the auth emulator explicitly on the127.0.0.1
, and then accessing the site usinglocalhost
, the auth url will be generated against127.0.0.1
which doesn’t map the login tolocalhost
.So if the site is hosted on
localhost
, try using:And if the site is hosted on
127.0.0.1
, try using:Hey everyone, apologies that the issue got closed. Thanks @jonathonadams for the additional information. It does look like a common factor that is causing the issue is accessing the app via
locahost
rather than127.0.0.1
.Let me notify our engineering team about the issue so that they can take a look into this and investigate the cause. That said, I’ll also be marking this as reproducible.