【发布时间】:2022-09-28 18:08:19
【问题描述】:
我正在使用react-native-auth0 实现身份验证,并且正在寻找一种方法来获取refreshToken 以及如何使用它来获取新的accessToken 和idToken 令牌。
标签: react-native auth0
我正在使用react-native-auth0 实现身份验证,并且正在寻找一种方法来获取refreshToken 以及如何使用它来获取新的accessToken 和idToken 令牌。
标签: react-native auth0
基本上我使用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 现在包含更新的 accessToken 和 idToken。如果您在 Auth0 仪表板中启用了 Refresh Token Rotation,您还将获得新的 refreshToken。
【讨论】: