auth0 checkSession({})在通过社交提供商登录时返回所需的登录名,但在通过用户名/密码登录时不返回所需的登录名

auth0 checkSession({})在通过社交提供商登录时返回所需的登录名,但在通过用户名/密码登录时不返回所需的登录名,auth0,Auth0,我有一个Angular应用程序,它使用Auth0进行身份验证,如果令牌尚未过期,我将尝试使用checkSession({},…)来持久化用户的会话 当我使用我为站点设置的用户名/pw登录时,当我重新加载浏览器/直接导航到资源时,这一切正常。但是,当我使用社交提供商(如Google)登录时,页面重新加载时的checkSession({},…)调用返回一个错误,并强制用户再次登录 一些相关代码(主要来自auth0教程): 导出类身份验证服务{ //创建Auth0 web auth实例 private

我有一个Angular应用程序,它使用Auth0进行身份验证,如果令牌尚未过期,我将尝试使用checkSession({},…)来持久化用户的会话

当我使用我为站点设置的用户名/pw登录时,当我重新加载浏览器/直接导航到资源时,这一切正常。但是,当我使用社交提供商(如Google)登录时,页面重新加载时的checkSession({},…)调用返回一个错误,并强制用户再次登录

一些相关代码(主要来自auth0教程):

导出类身份验证服务{
//创建Auth0 web auth实例
private _auth0=new auth0.WebAuth({
clientID:AUTH_CONFIG.CLIENT_ID,
域:AUTH_CONFIG.CLIENT_domain,
responseType:'令牌',
重定向URI:AUTH_CONFIG.REDIRECT,
受众:AUTH_CONFIG.audience,
作用域:AUTH_CONFIG.scope
});
accessToken:字符串;
userProfile:any;
expiresAt:编号;
//创建登录状态流以在整个应用程序中进行通信
loggedIn:布尔型;
loggedIn$=新行为主体(this.loggedIn);
登录:布尔;
isAdmin:布尔型;
//订阅令牌到期流
订阅:订阅;;
构造函数(专用路由器:路由器){
//如果应用验证令牌未过期,请请求新令牌
if(JSON.parse(localStorage.getItem('expires_at'))>Date.now(){
这个.renewToken();
}
}
...
handleAuth(){
//解析Auth0哈希时,获取配置文件
这是.\u auth0.parseHash((err,authResult)=>{
if(authResult&&authResult.accessToken){
window.location.hash='';
这是。_getProfile(authResult);
}否则如果(错误){
这个;
this.router.navigate(['/']);
错误(`error authentication:${err.error}`);
}
this.router.navigate(['/']);
});
}
private\u getProfile(authResult){
this.loggingIn=true;
//使用访问令牌检索用户的配置文件并设置会话
此.u auth0.client.userInfo(authResult.accessToken,(err,profile)=>{
如果(配置文件){
此._设置会话(authResult、profile);
这个;
}否则如果(错误){
warn(`检索配置文件时出错:${err.Error}`);
}
});
}
私有设置会话(authResult、profile?){
this.expiresAt=(authResult.expiresIn*1000)+Date.now();
//要在构造函数中访问的本地存储中的存储过期
setItem('expires_at',JSON.stringify(this.expiresAt));
this.accessToken=authResult.accessToken;
this.userProfile=profile;
如果(配置文件){
this.isAdmin=this.\u checkAdmin(配置文件);
}
...
}
...
get tokenValid():布尔值{
//检查当前时间是否超过访问令牌的到期时间
返回日期.now(){
if(authResult&&authResult.accessToken){
这是。_getProfile(authResult);
}否则{
这个;
}
});
}
}
(这是一项在应用程序中的许多地方调用的服务,包括一些路由守卫和一些依赖配置文件信息的组件。如果更多的应用程序代码有用,我可以提供。)


另请注意:AUTH_CONFIG.SCOPE='openid profile email'

因此,该问题似乎与我的应用程序没有任何关系。在使用社交提供商时,Auth0在其教程中有一个明确的注释,它确实帮助了我:

社交提供商的问题是,它们在我的Auth0仪表板中配置不正确,需要使用特定于提供商的应用程序密钥

重要提示:如果您在应用程序中使用Auth0社交连接, 请确保您已将连接设置为使用自己的连接 客户端应用程序密钥。如果您使用的是Auth0开发密钥,则令牌续订将 始终返回所需的登录名。每个社会关系的细节都有一个 链接有关如何获取您自己的密钥的明确说明 特别是国内流离失所者

在此页面上找到评论:

    export class AuthService {
  // Create Auth0 web auth instance
  private _auth0 = new auth0.WebAuth({
    clientID: AUTH_CONFIG.CLIENT_ID,
    domain: AUTH_CONFIG.CLIENT_DOMAIN,
    responseType: 'token',
    redirectUri: AUTH_CONFIG.REDIRECT,
    audience: AUTH_CONFIG.AUDIENCE,
    scope: AUTH_CONFIG.SCOPE
  });
  accessToken: string;
  userProfile: any;
  expiresAt: number;
  // Create a stream of logged in status to communicate throughout app
  loggedIn: boolean;
  loggedIn$ = new BehaviorSubject<boolean>(this.loggedIn);
  loggingIn: boolean;
  isAdmin: boolean;
  
  // Subscribe to token expiration stream
  refreshSub: Subscription;

  constructor(private router: Router) {
    // If app auth token is not expired, request new token
    if (JSON.parse(localStorage.getItem('expires_at')) > Date.now()) {
      this.renewToken();
    }
  }

...
  handleAuth() {
    // When Auth0 hash parsed, get profile
    this._auth0.parseHash((err, authResult) => {
      if (authResult && authResult.accessToken) {
        window.location.hash = '';
        this._getProfile(authResult);
      } else if (err) {
        this._clearRedirect();
        this.router.navigate(['/']);
        console.error(`Error authenticating: ${err.error}`);
      }
      this.router.navigate(['/']);
    });
  }

  private _getProfile(authResult) {
    this.loggingIn = true;
    // Use access token to retrieve user's profile and set session
    this._auth0.client.userInfo(authResult.accessToken, (err, profile) => {
      if (profile) {
        this._setSession(authResult, profile);
        this._redirect();
      } else if (err) {
        console.warn(`Error retrieving profile: ${err.error}`);
      }
    });
  }

  private _setSession(authResult, profile?) {
    this.expiresAt = (authResult.expiresIn * 1000) + Date.now();
    // Store expiration in local storage to access in constructor
    localStorage.setItem('expires_at', JSON.stringify(this.expiresAt));
    this.accessToken = authResult.accessToken;
    this.userProfile = profile;  
    
    if (profile) {
      this.isAdmin = this._checkAdmin(profile);
    }
    
    ...
  }
  
  
  ...

  get tokenValid(): boolean {
    // Check if current time is past access token's expiration
    return Date.now() < JSON.parse(localStorage.getItem('expires_at'));
  }

  renewToken() {
    // Check for valid Auth0 session
    this._auth0.checkSession({}, (err, authResult) => {
      if (authResult && authResult.accessToken) {
        this._getProfile(authResult);
      } else {
        this._clearExpiration();
      }
    });
  }
   

}