Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angular 2中为jwt令牌设置cookie_Javascript_Angular_Cookies - Fatal编程技术网

Javascript 如何在Angular 2中为jwt令牌设置cookie

Javascript 如何在Angular 2中为jwt令牌设置cookie,javascript,angular,cookies,Javascript,Angular,Cookies,我试图通过调用一个express api来验证Angular 2应用程序中的用户,该api在成功后提供JWT令牌。我有一个疑问要澄清 我们是让express设置cookie,还是用令牌设置cookie loginUser(email: string, password: string) { let headers = new Headers({ 'Content-Type': 'application/json'}); let options = new

我试图通过调用一个express api来验证Angular 2应用程序中的用户,该api在成功后提供JWT令牌。我有一个疑问要澄清

我们是让express设置cookie,还是用令牌设置cookie

    loginUser(email: string, password: string) {
        let headers = new Headers({ 'Content-Type': 'application/json'});
        let options = new RequestOptions({headers: headers});
        let loginInfo = { email: email, password: password };

        return this.http.post('/auth/login', JSON.stringify(loginInfo), options)
        .do(resp => {
            // Do I need to set the cookie from here or it from the backend?
        }).catch(error => {
            return Observable.of(false);
        })
    }

你必须用有角度的。我个人使用localStorage来实现这一点

我的身份验证服务中的示例:

login(email: string, password: string) {
    const body = { email, password };
    return this._http.post('http://localhost:8000/api/auth/authenticate', body)
        .map(response => {
            const jsonRes = response.json();
            if(jsonRes.status == 'success') {
                // Auth token
                localStorage.setItem('auth_token', jsonRes.data.token);
            }
            return jsonRes;
        })
        .catch(error => Observable.throw(error.json()));
}

你需要用角度来做。是的,您可以使用localStorage建议,但最好使用Cookie

下面是我在angular2应用程序中使用的代码示例

login.ts

auth-cookies-handler.ts

在组件中,您可以使用下面的行来验证AuthCookie

if (!_this._authCookie.getAuth()) {
    _this.router.navigate(["/login"]);
    return false;
}

我想我能胜任这项工作。您甚至可以使用本地存储。但请记住,如果有人听从您的建议。使用本地存储将为XSS打开存储在那里的内容。永远不要将任何被认为是秘密的东西存储在本地存储器中。@Reality Torrent在cookie中完成时如何?该应用程序是否仍然易受XSS攻击?是否有更简单的方法来实现这一点?@eugenunic-Yes。您可以简单地导入Cookie并使用get()和set()方法。但是我写这篇文章是因为我必须在我的应用程序中为常用类使用DI层。我已经使用过你的类,但是我做了一些额外的对象解析来检索对象键。Tnx
import { Injectable } from '@angular/core';
import { Cookie } from 'ng2-cookies/ng2-cookies';

@Injectable()
export class AuthCookie {
    constructor() { }

    getAuth(): string {
        return Cookie.get('id_token');
    }

    setAuth(value: string): void {
        //0.0138889//this accept day not minuts
        Cookie.set('id_token', value, 0.0138889);
    }

    deleteAuth(): void {
        Cookie.delete('id_token');
    }  
}
if (!_this._authCookie.getAuth()) {
    _this.router.navigate(["/login"]);
    return false;
}