本文整理汇总了TypeScript中angular2-jwt.JwtHelper类的典型用法代码示例。如果您正苦于以下问题:TypeScript JwtHelper类的具体用法?TypeScript JwtHelper怎么用?TypeScript JwtHelper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JwtHelper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getJwt
public getJwt(): any {
var jwtHelper = new JwtHelper();
var encodedToken = window.localStorage.getItem('jwt');
var jwt = jwtHelper.decodeToken(encodedToken);
return jwt;
}
示例2: isTokenValid
isTokenValid(token: string): Boolean {
// auth0 = new auth0.WebAuth({
// clientID: AUTH_CONFIG.CLIENT_ID,
// domain: AUTH_CONFIG.CLIENT_DOMAIN
// });
const jwtHelper: JwtHelper = new JwtHelper();
return jwtHelper.isTokenExpired(token);
}
示例3: ngOnInit
// Checks for token expiration to display appropriate page
ngOnInit() {
var jwtHelper = new JwtHelper();
var token = localStorage.getItem('auth_token');
if (token) {
console.log(jwtHelper.isTokenExpired(token));
if (jwtHelper.isTokenExpired(token)) {
this.router.navigate(['/']);
} else {
this.router.navigate(['/home']);
}
} else {
this.router.navigate(['/']);
}
}
示例4: tokenNotExpired
public static tokenNotExpired(tokenName?: string, jwt?: string) {
var authToken: string = tokenName || 'id_token';
var token: string;
if (jwt) {
token = jwt;
} else {
token = localStorage.getItem(authToken);
}
var jwtHelper = new JwtHelper();
if (!token || jwtHelper.isTokenExpired(token, null)) {
return false;
} else {
return true;
}
}
示例5: constructor
constructor(public router: Router, public http: Http, public authHttp: AuthHttp) {
let timer = Observable.timer(2000, 1000);
let jwtHelper: JwtHelper = new JwtHelper();
timer.subscribe(t => {
var token = jwtHelper.decodeToken(localStorage.getItem('id_token'));
if (token) {
this.tokenExpires = ((token.exp * 1000) - Number(new Date()));
this.tokenExpired = this.tokenExpires <= 0;
if (this.tokenExpires > -5000 && this.tokenExpires < 2000) {
this.refreshToken();
}
}
});
}
示例6: constructor
constructor(private _httpClient: HttpClient,
private _notificationService: NotificationService) {
this.baseUrl = API_ENDPOINT.concat('/auth/local/');
console.log(`baseurl : ${this.baseUrl}`)
this.jwtHelper = new JwtHelper();
//the user doesn't need to login as long as the token is valid
if (tokenNotExpired()) {
let idToken = localStorage.getItem('id_token');
let token = this.jwtHelper.decodeToken(idToken);
this.currentUser = new User();
this.currentUser._id = token._id;
this.currentUser.name = token.name;
this.currentUser.role = token.role;
console.log(`existing token - ${this.currentUser.name}`);
}
}
示例7: constructor
constructor(public router: Router, public http: Http, public authHttp: AuthHttp,
public jwtHelper: JwtHelper, private userService: UserService) {
this.jwt = localStorage.getItem('jwt');
this.decodedJwt = this.jwt && this.jwtHelper.decodeToken(this.jwt);
this.user = this.userService.getCurrentUser();
}
示例8: canActivate
canActivate() {
var token = localStorage.getItem("jwt");
if (token && !this.jwtHelper.isTokenExpired(token)){
console.log(this.jwtHelper.decodeToken(token));
return true;
}
this.router.navigate(["login"]);
return false;
}
示例9:
tokenGetter: () => {
// Look in session storage for id_token, but fallback to local storage
let session = sessionStorage.getItem('id_token');
let local = localStorage.getItem('id_token');
let token = session || local;
if (typeof token !== 'string') { return ''; }
// Remove any quotations from the sides
token = token.split('"').join('');
// Check validity of jwt token
if (token.split('.').length !== 3) {
localStorage.removeItem('id_token');
sessionStorage.removeItem('id_token');
return '';
}
// Check if token is expired. If it is, delete and send user to login page
if (jwtHelper.isTokenExpired(token)) {
sessionStorage.removeItem('id_token');
localStorage.removeItem('id_token');
this.router.navigate(['/login']);
return '';
}
return token;
},
示例10: Date
.mergeMap((token) => {
const decodedToken = this.jwtHelper.decodeToken(token);
return Observable.interval(
new Date(0).setUTCSeconds(decodedToken.exp) - new Date(0).setUTCSeconds(decodedToken.iat)
);
})
示例11:
this.authProvider.authUser.subscribe(jwt => {
if (jwt) {
const decoded = this.jwtHelper.decodeToken(jwt);
this.user = decoded.sub
}
else {
this.user = null;
}
});
示例12: canActivate
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (window.localStorage.getItem('token') &&
!jwtHelper.isTokenExpired(window.localStorage.getItem('token'))) {
return true;
}
this.router.navigate(['login']);
return false;
}
示例13: getUserName
getUserName(): string {
if (window.localStorage.getItem('token')) {
let token = window.localStorage.getItem('token');
let decoded = this.jwtHelper.decodeToken(token);
return decoded.login
} else {
return "not logged in"
}
}
示例14: isAuthenticated
isAuthenticated(): boolean {
let jwt = this.jwtStorage.getToken();
if (null === jwt) {
return false;
}
return !this.jwtHelper.isTokenExpired(jwt.token);
}
示例15: Number
timer.subscribe(t => {
var token = jwtHelper.decodeToken(localStorage.getItem('id_token'));
if (token) {
this.tokenExpires = ((token.exp * 1000) - Number(new Date()));
this.tokenExpired = this.tokenExpires <= 0;
if (this.tokenExpires > -5000 && this.tokenExpires < 2000) {
this.refreshToken();
}
}
});