Skip to content

Instantly share code, notes, and snippets.

@jtcrowson
Created March 7, 2019 22:49
Show Gist options
  • Save jtcrowson/5653c6cb9fb64cc52066e81dc818742d to your computer and use it in GitHub Desktop.
Save jtcrowson/5653c6cb9fb64cc52066e81dc818742d to your computer and use it in GitHub Desktop.
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