angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

getRedirectResult() always returns null (AF7+FB9)

Open capancheta opened this issue 2 years ago • 9 comments

Version info

Angular: 13.1.2

Firebase: 9.6.3

AngularFire: 7.2.0 / 7.3.0

How to reproduce these conditions

on my ngInit():

getRedirectResult().then(result => { console.log(result)})

Debug output

i always get null.

i tried using the /compat/auth, i get

{result : { credentials: null, user: null }}

Expected behavior

results should contain credentials and user.

Actual behavior

no results at all. Works properly in AF 6.1.4 + FB 8.2.1. Encountered after upgrade.

onAuthStageChanged() works; it detects the user correctly. Problem is, There is a subscription to auth,state already running on a different module (which i am prohibited to modify). Subscribing to onAuthStageChanged() creates some sort of pseudo-deadlock (sometimes it resolves, sometimes it doesnt).

My temp workaround is to use signInWithPopup() but that disables mobile compatibility. Tips on overcoming this are highly appreciated. Thank you

capancheta avatar Apr 11 '22 06:04 capancheta

I'm experiencing the same issue. Any ideas what can be wrong or how to rule out causes?

agustinhaller avatar May 03 '22 22:05 agustinhaller

Seeing the same. Angular 12.2.16, AF 7.3.0, FB 9.8.1, rxjs 6.6.7.

warrendodsworth avatar May 18 '22 05:05 warrendodsworth

This bug still exists in AF 7.4.1, FB 9.9.0. This prevents developers from using signInWithRedirect and linkWithRedirect on mobile devices as recommended.

BL-marcusrogers avatar Nov 03 '22 01:11 BL-marcusrogers

I faced the same issue. My project is Angular 13, AF 7.2, Firebase 9.

I used this link as reference. https://stackoverflow.com/questions/54285127/getredirectresult-is-returning-null-after-redirect-login

I realized that whenever user is null, it emits onAuthStateChanged after the return of getRedirectResult(in the normal case, onAuthStateChanged comes first).

It's true. So I changed sampleAuthService.ts below.

@Injectable({
  providedIn: 'root',
})
export class SampleAuthService {
  constructor(
    private afAuth: AngularFireAuth,
  ) {
    // getRedirectResult after observe on onAuthStateChanged
    this.afAuth.onAuthStateChanged((state) => {
      // something process
    });
  }

  public async init() {
    try {
      const result = await this.afAuth.getRedirectResult();
    } catch(err) {
     // something err handling
    }
  }

and sampleAuthService DI in appInitialerService, and call init(); in it. https://angular.io/api/core/APP_INITIALIZER

The important points is getRedirectResult after observe on onAuthStateChanged. I hope you can fix it soon.

komura-c avatar Dec 15 '22 14:12 komura-c

@komura-c I've tried your suggestion, but none of the following seems to work:

  async init() {
    this.logger.debug("Checking for token from redirect");
    const credentials = await getRedirectResult(this.auth);
    this.idToken = await credentials?.user.getIdToken();

    await this.auth.onAuthStateChanged(
      async () => {
        const credentials = await getRedirectResult(this.auth);
        this.idToken = await credentials?.user.getIdToken();
        console.log("inner");
        console.log(this.idToken);  // Is null
      }
    );
    console.log("outer");
    console.log(this.idToken);  // Is null as well
  }
export const AUTH_ID_TOKEN_INITIALIZER_PROVIDER: Provider = {
  provide: APP_INITIALIZER,
  useFactory: (logger: NGXLogger, authService: AuthService) => {
    return async () => {
      logger.info("Initializing App: Waiting for (API) idToken")
      await authService.init();
      return firstValueFrom(authService.idToken$);
    }
  },
  deps: [NGXLogger, AuthService],
  multi: true
};

I keep getting null.

I am testing this on my local Chrome browser (desktop) as well as on a mobile phone (also Chrome).

Is there a chance that this is just not working because I am testing this using an invalid SSL certificate during development?

falk-stefan avatar Apr 13 '23 10:04 falk-stefan

I'm also getting the same issue, I can't use signInWithRedirect. I get a null user profile after the redirect. Did you find out the root cause? I'm using an authDomain that is on a different subdomain where the login page is hosted.

The user profile should propagate across different domains of the same domain, right?

jhades avatar Apr 27 '23 13:04 jhades

@jhades It's pretty complicated and I don't know where to start. It really depends on your setup but if you have a custom authDomain you want to pay https://firebase.google.com/docs/auth/web/redirect-best-practices a visit and apply what works for you.

Note that working with Emulators or a server on your localhost, wich you might want to use as authDomain,

Also: If you are on Google Cloud Platform like me, you have to make sure that you're authorizing your domains (see https://console.cloud.google.com/apis/credentials/oauthclient):

image

falk-stefan avatar Apr 27 '23 14:04 falk-stefan

I'm getting this issue but with react

rikotacards avatar Jun 08 '23 08:06 rikotacards