本文整理匯總了TypeScript中angular2-jwt/angular2-jwt.JwtHelper類的典型用法代碼示例。如果您正苦於以下問題:TypeScript JwtHelper類的具體用法?TypeScript JwtHelper怎麽用?TypeScript JwtHelper使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了JwtHelper類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: getCurrentUser
getCurrentUser(token: string):Observable<User>{
let jwtContent = this._jwtHelper.decodeToken(token);
return this._userService.getUser(jwtContent.uri).map( (user:User) => {
return user;
});
}
示例2: Error
.flatMap((res) => {
if (res.status == 200) {
let body = res.json();
let token = body.key;
let jwtContent = this.jwtHelper.decodeToken(token);
this.saveToken(token);
return this._userService.getUser(jwtContent.url).map((user:User) => {
this._authContext.setUser(user);
return user;
});
} else if (res.status == 401) {
// TODO: more meaningful error
throw new Error();
} else {
// TODO: more meaningful error
throw new Error();
}
});
示例3: canActivate
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (window.localStorage.getItem('token') &&
!jwtHelper.isTokenExpired(window.localStorage.getItem('token')) &&
this.userService.isAdmin()) {
return true;
}
this.router.navigate(['login']);
return false;
}
示例4: if
.flatMap( (res) => {
if (res.status == 200) {
let body = res.json();
let token = body.token;
let jwtContent = this._jwtHelper.decodeToken(token);
this.saveToken(token);
return this._userService.getUser(jwtContent.uri).map( (user:User) => {
return {success:true, user:user};
});
} else if (res.status == 401){
} else if (res.status == 404) {
}
}
示例5: tryLogin
/**
* Try logging in using authentication token accessible to the app
* (through e.g localStorage, sessionStorage or other mechanisms)
*/
public tryLogin() {
if (!this._authContext.isLoggedIn()) {
let jwtToken = this.getToken();
if (jwtToken != null) {
let jwtContent = this.jwtHelper.decodeToken(jwtToken);
this._userService.getUser(jwtContent.url).subscribe(user => {
this._authContext.setUser(user);
}, error => {
});
} else {
this._authContext.setUser(null);
}
}
}
示例6: isLogged
/**
* Checks if the user session is active
*
* @returns {boolean}
*/
public isLogged(): boolean {
let token: string = localStorage.getItem("id_token");
return token && !this.jwtHelper.isTokenExpired(token);
}