angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.7k stars 2.19k forks source link

getRedirectResult() always returns null (AF7+FB9) #3208

Open capancheta opened 2 years ago

capancheta commented 2 years ago

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

agustinhaller commented 2 years ago

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

warrendodsworth commented 2 years ago

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

BL-marcusrogers commented 2 years ago

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.

komura-c commented 1 year ago

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.

falk-stefan commented 1 year ago

@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?

jhades commented 1 year ago

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?

falk-stefan commented 1 year ago

@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

rikotacards commented 1 year ago

I'm getting this issue but with react

servefast-cto commented 5 months ago

Same issue with office addin and chrome