Skip to content

Instantly share code, notes, and snippets.

@ova2
Last active April 3, 2019 06:37
Show Gist options
  • Save ova2/d65be135d3e79b0ce7634a54022d0ed5 to your computer and use it in GitHub Desktop.
Save ova2/d65be135d3e79b0ce7634a54022d0ed5 to your computer and use it in GitHub Desktop.
@Injectable({
providedIn: 'root'
})
export class LoggedInGuard implements CanActivate {
protected constructor(private _router: Router, _store: CoreStore) { }
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return this._store.ready$.then(() => this.toResult(), () => this.toResult());
}
private toResult(): boolean {
const isLoggedIn = !!this._store.state.authToken;
if (!isLoggedIn) {
this._router.navigate(['/login']);
}
return isLoggedIn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment