angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

AngularFireAuthGuards kill change detection

Open Wykks opened this issue 6 years ago • 13 comments

Version info

Angular: 9.1.4

Firebase: 7.14.2

AngularFire: 6.0.0

Other (e.g. Ionic/Cordova, Node, browser, operating system): Ionic (web only)

How to reproduce these conditions

Failing test unit, Plunkr, or JSFiddle demonstrating the problem

TBD, not sure how to minimally reproduce...

Steps to set up and reproduce

Sample data and security rules

Debug output

** Errors in the JavaScript console ** None

** Output from firebase.database().enableLogging(true); **

** Screenshots **

Expected behavior

Change detection should be working. Old issue : https://github.com/angular/angularfire/issues/2367. This is not entirely fixed. You can check here https://github.com/angular/angularfire/blob/76104ef4e2193bab72caff0f1a8f7655ccbf51f8/src/auth-guard/auth-guard.ts#L43 Add a NgZone.assertInAngularZone(). And we get "Expected to be in Angular Zone, but it is not!".

Actual behavior

Random issues, due to change detection not working. I was blaming ionic to break change detection but as soon as I removed the AngularFireAuthGuard, everything started to behave correctly...

Workaround

Create a custom AuthGuard like this:

@Injectable({
  providedIn: 'root',
})
export class AuthGuard extends AngularFireAuthGuard implements CanActivate {
  constructor(
    @Inject(FIREBASE_OPTIONS) options: FirebaseOptions,
    @Optional()
    @Inject(FIREBASE_APP_NAME)
    nameOrConfig: string | FirebaseAppConfig | null | undefined,
    zone: NgZone,
    router: Router
  ) {
    super(options, nameOrConfig, zone, router);
    this.authState = this.authState.pipe(
      observeOn(new ɵAngularFireSchedulers(zone).insideAngular)
    );
  }
}

Wykks avatar May 04 '20 15:05 Wykks

Having the same issue (Angular 9.1.4 in browser)

ghigi123 avatar May 07 '20 07:05 ghigi123

Same issue. Angular: 9.1.3 AngularFire: 6.0.0-rc.2

field123 avatar May 07 '20 12:05 field123

@Wykks thanks for the workaround.

jcpham81 avatar May 09 '20 15:05 jcpham81

Ive tried the workaround and it is working related to AuthGuards but AngularFireFunctions break change detection too.

crtl avatar May 15 '20 13:05 crtl

@Wykks Thanks for the workaround! I spent a full day on that.

BiserSirakov avatar May 20 '20 22:05 BiserSirakov

There's still a team working on it? The latest updates are full of bugs like this.

BianchiSeb avatar May 28 '20 20:05 BianchiSeb

Is this solved, and does the workaround indeed break the AngularFireFunctions?

rami-alloush avatar Jun 12 '20 17:06 rami-alloush

The workaround doesn't work with custom AuthPipe

magoarcano avatar Jun 13 '20 16:06 magoarcano

Of course, it's just a workaround for the Guard. It's not going to work for something else. Also, no it doesn't break AngularFireFunctions at all. It's super weird to not have any news about this since more than a whole month. What's going on google?

Wykks avatar Jun 13 '20 20:06 Wykks

@Wykks thanks for the workaround, it works great for me! this issue was driving me crazy

bizio avatar Jun 18 '20 13:06 bizio

Hello angularFire team. I literally spend a ton of money in your app for my clients. At least answer us. It’s a shame that core features are broken. It’s a shame you don’t manage the PR. At least give us back the projets lead and we can manage it like a TRUE open source project.

BianchiSeb avatar Jul 05 '20 17:07 BianchiSeb

This should be addressed in a recent release, if still having trouble feel free to open a new issue & we absolutely welcome PRs.

jamesdaniels avatar Oct 30 '20 20:10 jamesdaniels

If someone has the same problem, was not really obvious. For me was throwing the cyclic dependency on services that depended on AngularFirestore but not in the guard indeed. When I commented my AuthGuard the error disappeared and noticed that Firestore has its own guards: https://github.com/angular/angularfire/blob/master/docs/auth/router-guards.md. Implemented those guards and now everything is working fine.

dmeneses avatar Mar 08 '21 00:03 dmeneses