【问题标题】:How to refresh auth tokens with react-native-auth0?如何使用 react-native-auth0 刷新身份验证令牌?
【发布时间】:2022-09-28 18:08:19
【问题描述】:

我正在使用react-native-auth0 实现身份验证,并且正在寻找一种方法来获取refreshToken 以及如何使用它来获取新的accessTokenidToken 令牌。

    标签: react-native auth0


    【解决方案1】:

    基本上我使用auth0.passwordRealm 来实现使用电子邮件/密码登录:

    import Auth0 from 'react-native-auth0';
    ...
    const config = {
        username: email,
        password: password,
        realm: 'Username-Password-Authentication',
        scope: 'openid profile email offline_access',
        audience: 'https://' + Auth0Config.domain + '/userinfo',
    };
    const credentials = await auth0.auth.passwordRealm(config);
    

    Auth0 不会向您发送 refreshToken,直到您将 offline_access 放入范围属性中。在我的情况下,accessToken 存在 24 小时,这在凭据的 expiresIn 属性中有所提及。现在我们可以使用 refreshToken 来请求新的令牌。

    const newCredentials = await auth0.auth.refreshToken({
      refreshToken: credentials.refreshToken,
      scope: 'openid profile email offline_access',
    });
    

    newCredentials 现在包含更新的 accessTokenidToken。如果您在 Auth0 仪表板中启用了 Refresh Token Rotation,您还将获得新的 refreshToken

    【讨论】:

      猜你喜欢
      • 2019-12-08
      • 2017-11-08
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2019-08-30
      • 2020-07-07
      相关资源
      最近更新 更多