本文整理匯總了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);
}