Created
December 27, 2018 18:10
-
-
Save whisher/e79dd9f58abd4420aed936fe67311ee7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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