本文整理汇总了TypeScript中ng2-cookies.Cookie类的典型用法代码示例。如果您正苦于以下问题:TypeScript Cookie类的具体用法?TypeScript Cookie怎么用?TypeScript Cookie使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Cookie类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: resetUser
resetUser(): void {
Cookie.delete('crsftoken');
this.removeFromStorage('bhmc_token');
this._currentUser = new User();
this.currentUserSource.next(this._currentUser);
this.saveToStorage('bhmc_user', JSON.stringify(this._currentUser));
this.errorHandler.clearUserContext();
}
示例2: canActivate
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
// 存储appToken到session
if (!Cookie.get(CommonConfig.AUTH_TOKEN)) {
this.router.navigate(['/user/login']);
return false;
} else {
return true;
}
}
示例3: createHeaders
private createHeaders(contentType: string): Headers {
let headers = new Headers({'Content-Type': contentType});
let token = localStorage.getItem('bhmc_token');
if (!token) {
token = sessionStorage.getItem('bhmc_token');
}
if (token) {
headers.append('Authorization', 'Token ' + token);
}
// This cookie is added to responses by Django
let csrf = Cookie.get('csrftoken');
if (csrf) {
headers.append('X-CSRFToken', csrf);
}
return headers;
}
示例4: login
/**
* 登录
*/
login(param: LoginParamModel) {
console.log(`用户 ${param.username} 正在登录...`);
Cookie.set(CommonConfig.AUTH_USER_NAME, param.username);
const params = HttpUtils.getParams();
params.append('username', param.username);
params.append('password', param.password);
params.append('grant_type', CommonConfig.AUTH_GRANT_TYPE);
params.append('client_id', CommonConfig.AUTH_CLIENT_ID);
const options = HttpUtils.getDefaultRequestOptionsByClient();
options.params = params;
return this.http.post(ApiConfig.AUTHORIZATION_API, param.toJson(), options)
.map(res => res.json())
.subscribe(
data => {
this.saveToken(data);
return true;
},
err => {
CookieUtils.clearBy(CommonConfig.AUTH_USER_NAME);
this.toastyService.error('登录失败, 请检查您的用户名或密码');
return false;
});
}
示例5: getUser
public static getUser() {
const data = Cookie.get(CommonConfig.AUTH_USER_INFO);
if (data.length > 0) {
return JSON.parse(data);
}
}
示例6: logout
logout() {
Cookie.delete('access_token');
this._router.navigate(['/login']);
}
示例7: checkCredentials
checkCredentials(){
if (!Cookie.check('access_token')){
this._router.navigate(['/login']);
}
}
示例8: getResource
getResource(resourceUrl) : Observable<Foo>{
var headers = new Headers({'Content-type': 'application/x-www-form-urlencoded; charset=utf-8', 'Authorization': 'Bearer '+Cookie.get('access_token')});
var options = new RequestOptions({ headers: headers });
return this._http.get(resourceUrl, options)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
示例9: saveToken
saveToken(token){
var expireDate = new Date().getTime() + (1000 * token.expires_in);
Cookie.set("access_token", token.access_token, expireDate);
console.log('Obtained Access token');
this._router.navigate(['/']);
}
示例10: onLanguageChange
onLanguageChange(language: any) {
this.translate.use(language.locale);
Cookie.set(AppConfig.languageCookieName, language.locale, null, "/");
this.languageService.onLanguageSet(language.locale);
}