【问题标题】:how to get jwt token from access token in auth0如何从 auth0 中的访问令牌中获取 jwt 令牌
【发布时间】:2018-01-13 03:22:56
【问题描述】:

我正在开发 angular2-nodejs-mongodb 单页应用程序。并使用 auth0 安全性。

我从 angular2 项目中获得了 access_token。但它不是 jwt_token。主要问题是 node.js 项目需要 jwt_token 像 Authorization : Bearer My_Token。下面附上图片。

角度图像:获取 access_token

postman:send 要求 auth0 获取 jwt_token

邮递员:尝试访问我的 nodejs 应用程序

其实我不明白,如何将角度访问令牌转换为 node.js jwt_token

【问题讨论】:

标签: node.js angular token jwt auth0


【解决方案1】:

假设我在互联网上找到了这个解决方案

实际上是从私人仓库中复制了一些代码)

const createClient = () => createAuth0Client({
  domain: AUTH0_DOMAIN,
  client_id: AUTH0_ID,
  redirect_uri: window.location.origin,
});

...

export class AuthService {

  constructor(
    public sessionService: SessionService,
    public alertService: AlertService,
    public userService: UserService,
  ) {
    makeObservable(this, {
      sessionService: observable,
      alertService: observable,
      userService: observable,
      authPopup: action.bound,
      authRedirect: action.bound,
      _handleNewToken: action.bound,
      init: action.bound,
    });
    this.init();
  }

  async _handleNewToken(auth0: Auth0Client) {
    const {
      _raw: jwt,
      nickname,
      picture,
      email,
    } = await auth0.getIdTokenClaims();
    nickname && this.userService.setNickname(nickname);
    picture && this.userService.setPicture(picture);
    email && this.userService.setEmail(email);
    this.sessionService.setSessionId(jwt);
    //                               ^^^
  }

  async authPopup() {
    let token: string | false = false;
    try {
      const auth0 = await createClient();
      await auth0.loginWithPopup();
      await this._handleNewToken(auth0);
      this.alertService.push('Signed in successfully');
    } catch (e) {
      console.log(e);
      this.alertService.push('Authorization failed');
    } finally {
      return token;
    }
  }

  async authRedirect() {
    try {
      const auth0 = await createClient();
      await auth0.loginWithRedirect();
      await sleep(5_000);
    } catch(e) {
      console.log(e);
      this.alertService.push('Authorization failed');
    }
  }

  async init() {
    try {
      const auth0 = await createClient();
      await auth0.handleRedirectCallback();
      await this._handleNewToken(auth0);
      this.alertService.push('Signed in successfully');
    } catch (e) {
      console.log(e);
    }
  }

};

...

const handleButtonClick = () => {
        await authService.authRedirect();
        ...
};

【讨论】:

    猜你喜欢
    • 2017-12-29
    • 2021-10-20
    • 2019-11-08
    • 2019-07-20
    • 1970-01-01
    • 2019-09-09
    • 2019-12-08
    • 2021-04-21
    • 2021-07-12
    相关资源
    最近更新 更多