5

i have an issue with my oauth setup, when i logout with this effect:

@Effect()
  logout:  Observable<Action> = this.actions.ofType(UserActions.LOGOUT)
    .switchMap(() => Observable.of(this.afAuth.auth.signOut()))
    .map(() => new UserActions.GetUser())
    .catch(err => Observable.of(new UserActions.AuthError({error: err.message})));

Everything works fine and the UserActions.GetUser() is being called. Now if i try to log in with this effect and this firebase auth function:

@Effect()
  googleLogin: Observable<Action> = this.actions.ofType(UserActions.GOOGLE_LOGIN)
    .switchMap(() => Observable.fromPromise(this.loginWithGoogle()))
    .map(() => new UserActions.GetUser())
    .catch(err => Observable.of(new UserActions.AuthError({error: err.message})));

private loginWithGoogle() {
  return this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
}

The new UserActions.GetUser() is called and i can see it in the Redux DevTools, but the actual effect is not called, i put a lot of console.log's in there to see if i did something wrong in the function, but it isn't called at all and also i have a catch in the GetUser, but that's also not triggering.

Is there some issue with firebase in this case or am i stupid?

Actions:

Dispatched Actions

Get User effect:

@Effect()
  getUser: Observable<Action> = this.actions.ofType(UserActions.GET_USER)
    .switchMap(() => {
      console.log('test'); // <-- Not called
      return this.afAuth.authState;
    })
    .map(()=> {
      return new UserActions.Authenticated();
    });

** EDIT 1

I made a new observation: When there is no internet connection anymore, so firebase loads from local, it actually works, somehow there is an issue with the connection to the firebase database

Leon Marzahn
  • 341
  • 4
  • 15

3 Answers3

0

try to use this one:

@Effect()
googleLogin: Observable<Action> = this.actions.pipe(

        ofType(UserActions.GOOGLE_LOGIN),
        map(action => action.payload), // if there is no payload disregard this
        exhaustMap((auth: any) => Observable.fromPromise(this.loginWithGoogle()
               .pipe(
                  map(() => new UserActions.GetUser()),
                  catchError(err => Observable.of(new UserActions.AuthError({error: err.message})))
            ))
        )

    )

for this a little difference between exhaustMap and switchMap. let me know if you succeed to work it.

m.akbari
  • 572
  • 8
  • 22
  • also, this is a nice explanation regarding exhaustMap and switchMap: https://stackoverflow.com/questions/49698640/flatmap-mergemap-switchmap-and-concatmap-in-rxjs/49723075#49723075 – m.akbari May 30 '18 at 11:00
  • Unfortunately no, no effect at all. Still the same behaviour as before. – Leon Marzahn May 30 '18 at 18:46
  • Are you sure loginWithGoogle returns a promise? you can return from loginWithGoogle as observable and get rid of observable.fromPromise(). maybe something shady happens in there. if you resolve the problem please let me know how. good luck – m.akbari May 31 '18 at 07:51
0

can you try this one? It might help in my opinion.

  @Effect()
  googleLogin = this.actions.ofType(UserAction.GOOGLE_LOGIN).pipe(
    map(() => Observable.fromPromise(this.loginWithGoogle()),
    mergeMap(()=> return[{type: UserActions.GET_USER}])
  );
Maciej Wojcik
  • 2,115
  • 2
  • 28
  • 47
0

I ran into a similar issue, I think the effect is not being called because in your getUser effect you are not implementing catch block if the effect throws any error, so ideally in this case if an effect gets an error and it doesn't have any catch mechanism the effect won't invoke for further actions.

Jaya Krishna
  • 313
  • 4
  • 14
  • But i have a catch function in `getUser`, and it doesn't get called. – Leon Marzahn Jun 06 '18 at 08:09
  • `@Effect() getSearchData: Observable = this.actions .ofType() .map() .switchMap() .mergeMap(result => { return []; }) .catch(e => Observable.of(new actions.ApiError(e))) );` Usually, this is how u frame my effects to get them invoked multiple times, if you keep the catch block outside again it won't work for multiple actions. – Jaya Krishna Jun 06 '18 at 08:38
  • My `getUser` effect is built exactly like that, but it somehow breaks with `signInWithPopup` – Leon Marzahn Jun 06 '18 at 13:07
  • strange, @PsychoPflanze is it fixed now, are you still facing the same. – Jaya Krishna Jun 08 '18 at 13:53
  • No, it still doesn't work. I am thinking about making my own backend – Leon Marzahn Jun 09 '18 at 14:52