【问题标题】:Angular2 JWT Authentication - JWTs must come in 3 parts?Angular2 JWT 身份验证 - JWT 必须分为 3 个部分?
【发布时间】:2016-07-15 21:44:20
【问题描述】:

我正在使用 AngularJS 2 开发一个简单的 MEAN 堆栈应用程序。我正在使用 passportJSjwt 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;
      });
  }

但是,当我提交登录按钮时,我收到此错误:

未捕获(承诺):错误:JWT 必须有 3 个部分

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

【问题讨论】:

    标签: angular passport.js jwt


    【解决方案1】:

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

    您可以在这里粘贴:http://jwt.io/

    这个网站可以告诉你它是否是一个有效的 JWT 并且可以解码它的内容,所以你可以证明它是正确的。

    【讨论】:

      【解决方案2】:

      我有同样的错误。我解决了错误。 在 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' });
      

      很高兴为您提供帮助

      【讨论】:

        猜你喜欢
        • 2017-10-31
        • 2021-04-20
        • 2017-05-18
        • 2018-02-04
        • 2020-03-18
        • 2020-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多