0

I am trying to implement simple user authentication, and currently I am working on preventing user logout on page refresh. For that I am using defer . First time when app is opened defer return AuthActions.Logout() which is correct because there is no authentication the problem is with refresh because although token is set in a chrome storage and all actions are dispatched effect is not called again.

@Effect()
init$ = defer((): Observable<AuthActions.SigninUser | AuthActions.Logout> => {
    const token = this.authService.getToken();
    console.log('in def')
    return (token)
        ? of(new AuthActions.SigninUser())
        : of(new AuthActions.Logout())
});

then it goes here ->

@Effect()
            login$ = this.actions$
                .pipe(
                    ofType(AuthActions.SIGNIN_USER),
                    switchMap(() => {
                        console.log('signin user')
                        return [new UserDetailsActions.GetUserDetails];
                    }),
                )

if token is set (which is) this should be fired but it is fired only once when user sign in ->

 @Effect()
    getUserById = this.actions$
        .pipe(
            ofType(UserDetailsActions.GET_USER_DETAILS),
            mergeMap(() => {
                let token = this.authService.getTokenUsername();
                return this.http.get(`${AppProperties.API_URL}user/${token}`)
            }),
            concatMap((userDetails: UserDetails) => {
                sessionStorage.setItem('currentUserId', userDetails.id.toString());
                return [new UserDetailsActions.SetUserDetails(userDetails)]
            })
        );

Any help would be appreciated

Mateusz Gebroski
  • 1,274
  • 3
  • 26
  • 57

0 Answers0