Skip to content

Instantly share code, notes, and snippets.

@whisher
Created December 27, 2018 18:10
Show Gist options
  • Save whisher/e79dd9f58abd4420aed936fe67311ee7 to your computer and use it in GitHub Desktop.
Save whisher/e79dd9f58abd4420aed936fe67311ee7 to your computer and use it in GitHub Desktop.
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate, CanLoad {
constructor(private store: Store<AuthState>) {}
canActivate(): Observable<boolean> {
return this.store.pipe(
select(selectIsAuthenticated),
map(isValidToken => {
if (!isValidToken) {
this.store.dispatch(new Logout());
return false;
}
return true;
}),
take(1)
);
}
canLoad(): Observable<boolean> {
return this.store.pipe(
select(selectIsAuthenticated),
map(isValidToken => {
if (!isValidToken) {
this.store.dispatch(new Logout());
return false;
}
return true;
}),
take(1)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment