Skip to content

Instantly share code, notes, and snippets.

@jtcrowson
Created March 7, 2019 22:49
auth-guard.service.ts from version 7.3.0 of ngrx/platform
// NgRx v7.3.0
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private store: Store<fromAuth.State>) {}
canActivate(): Observable<boolean> {
return this.store.pipe(
select(fromAuth.getLoggedIn),
map(authed => {
if (!authed) {
this.store.dispatch(new AuthApiActions.LoginRedirect());
return false;
}
return true;
}),
take(1)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment