tomastrajan / angular-ngrx-material-starter

Angular, NgRx, Angular CLI & Angular Material Starter Project
https://tomastrajan.github.io/angular-ngrx-material-starter
MIT License
2.83k stars 917 forks source link

No redirect from /examples/authenticated if not login. #523

Open daboudii opened 4 years ago

daboudii commented 4 years ago

Minimal reproduction of the bug with instructions:

1- Remove all data from localstorage. 2- Navigate to: https://tomastrajan.github.io/angular-ngrx-material-starter#/examples/authenticated 3- No component/route is loaded after

Expected behavior:

Redirect to /example or /about page.

Other information:

Something like this could fix the issue

export class AuthGuardService implements CanActivate {
  constructor(private store: Store<AppState>, private router: Router) {}

  canActivate(): Observable<boolean> {
    this.store.pipe(select(selectIsAuthenticated)).subscribe(isAuth => {
      if (isAuth) {
        return true;
      } else {
        this.router.navigateByUrl('/');
         return false;
      }
    })

    return this.store.pipe(select(selectIsAuthenticated));
  }
}

I would be willing to submit a PR to fix this issue:

[x] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No