Angular2 JWT 身份验证 - JWT 必须分为 3 个部分?

Angular2 JWT Authentication - JWTs must come in 3 parts?

我正在开发一个简单的 MEAN 堆栈应用程序,使用 AngularJS 2。我正在使用 passportJS with the jwt strategy + angular2-jwt 帮助程序库。

这是我的 Angular 代码,它处理登录代码。我已经测试了 API 并且它可以工作(与邮递员一起使用)。

//login.component.ts:
 onSubmit(credentials):void {
      console.log("on SUbmit here");
    this._userService.login(credentials.email, credentials.password)
    .subscribe((result) => {
      if (result) {
        console.log("Link to Todo?");
        this._router.navigate(['TodoComponent']);
      }
    });
  }

//user.service.ts:
  signUp(firstName, lastName, email, password) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

      let user = new URLSearchParams();
        user.set('email', email);
        user.set('password', password);
        user.set('firstName', firstName);
        user.set('lastName', lastName);

    return this._http
      .post('/signup', user.toString(), { headers })
      .map(res => res.json())
      .map((res) => {
        console.log(res.success);
        return res.success;
      });
  }

但是,当我提交登录按钮时,我得到这个错误:

Uncaught (in promise): Error: JWT must have 3 parts

我应该怎么做才能确保以正确的格式提供 JWT 并避免这些错误?

你的 JWT 长什么样?您的 JWT 应该有 2 dots,它将字符串拆分为 3 parts。通常第一和第二部分以 ey 开头,这是 {.

的 base64 编码值

您可以将其粘贴到此处:http://jwt.io/

此站点可以告诉您它是否是有效的 JWT 并且可以解码其内容,因此您可以证明它是正确的。

我有同样的错误。我解决了错误。 在 auth0 中创建新的 API。给出唯一标识符名称。我拍了 "NodeAPI" 我在下面提到配置代码。

auth0 = new auth0.WebAuth({ 
             clientID: 'Hg3EhAWKgrPrX5UNGqFQA5vTbVGWF', 
             domain: 'xyz.auth0.com', 
             responseType: 'token id_token', 
             audience: 'NodeAPI', 
             redirectUri: 'http://localhost:4200/callback', 
             scope: 'openid' });

很高兴为您提供帮助