0

PROBLEM:

After login to the app when I click on signOut button, it successfully signOut and redirect to login page. But when I refresh a page then try signing out then it will do nothing. Anyone knows what can be issue?

CODE:

// action
export class Logout implements Action {
  readonly type = LOGOUT;
}

// effect
@Effect()
  logout$ = this.actions$
    .ofType(authAction.LOGOUT)
    .pipe(
      map( () => {
        debugger;
        this.authService.logoutUser();
        return new fromRoot.Go({
          path: ['/account/login'],
        });
      })
  );

// calling action on signout 
public signOut() {      
      this.store.dispatch(new fromStore.Logout());
    }

SCREENSHOT of Chrome Redux DevTools

Before refreshing the page: http://prntscr.com/ne3pc0

After refreshing the page: http://prntscr.com/ne3qpr (There is nothing after Logout Action)

0

Try this

import { throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';    
// effect
@Effect()
logout$ = this.actions$.pipe(
.ofType(authAction.LOGOUT),
switchMap(() => {
    return this.authService.logoutUser()
        .pipe(map(() => {
            return new fromRoot.Go({path: ['/account/login']})
         }), 
         catchError((error: any) =>{
            return of(error)    
         }), 
         finalize(() => {
            //do something like hide loader
         }))
  })  
);

let me know in case of any issue

  • It would be worth adding some explanation as to why you believe this will work. Also I think you meant to say .pipe(map(() => ...)) presuming rxjs6 is being used – Michael Apr 19 at 6:57
  • I have tested it and its working. Also it can be done using .pipe too – Pawan Jangid Apr 19 at 7:11
  • I have updated my solution with pipes – Pawan Jangid Apr 19 at 7:51
  • @PawanJangid it's not working. I have tried by adding debugger in the effect but after refreshing the page the control not go inside the effect after action. – Johar Zaman Apr 19 at 10:33
  • @Johar Zaman: Can you try one scenario Login->Logout->Login->Logout without refreshing the page...And after that refresh the page and check console for any errors – Pawan Jangid Apr 19 at 11:03 

来自  https://stackoverflow.com/questions/55756910/logout-action-is-not-working-after-reloading-angular-page