2

我是 auth0 和 Angular 2 的初学者。我正在尝试使用 Auth0 对我的应用程序进行身份验证和授权。在我用来初始化身份验证的代码下方

@Injectable()
export class Auth {
  //Configure Auth0
  lock = new Auth0Lock('CLIENT_ID','<myapp>.eu.auth0.com', {
    auth: {
      params: {
        scope: 'openid profile user_metadata app_metadata',
        audience: 'https://<myapp>.eu.auth0.com/api/v2/'
      }
    }
  });

  userProfile;

  constructor(private router: Router) {

    this.userProfile = JSON.parse(localStorage.getItem("profile"));

    // Add callback for lock `authenticated` event
    this.lock.on("authenticated", (authResult) => {
      localStorage.setItem('id_token', authResult.accessToken);

      console.log(authResult);

      this.lock.getUserInfo(authResult.accessToken, (error, profile) => {
        if (error) {
          console.log("Error:", error);
          return;
        }

        localStorage.setItem("profile", JSON.stringify(profile));

        this.userProfile = profile;


      });
    });
  }

当我记录配置文件时,我在配置文件 json 中看不到 app_metadata,在 JWT 令牌中也看不到它。

数组[5] 0:"sub" 1:"name" 2:"nickname" 3:"picture" 4:"updated_at"

我需要它从用户配置文件中获取角色。

首先,我使用了 getProfile 方法,它返回了正确的元数据,但我读到它会被弃用,所以我用 getUserInfo 替换它,但它以不同的方式工作。

我注意到,如果我删除观众参数,authResult 包含 app_metadata 信息,如角色,但我从控制台收到“JWT 必须有 3 个部分”错误。

关于它是如何工作的,我想念一些东西。

请问,有人可以帮我吗?谢谢。

4

0 回答 0