【发布时间】:2019-07-28 15:45:35
【问题描述】:
我正在使用 Expo 和 AWS Cognito 和 AWS Amplify 构建一个 React Native 应用程序,并且我正在尝试使用 AWS 启用 Facebook、Google 等登录
我可以毫无问题地创建用户并使用 Cognito API 登录。
不过,使用第三方需要使用 Expo AuthSession 功能。
该功能本身运行良好,我可以一直从 /oauth2/token 端点检索正确的令牌。
但是,就 Amplify 而言(我知道),用户没有登录,所以当我尝试获取 Auth.currentAuthenticatedUser() 时,返回 null。
// Open URL in a browser
openURL = async (url) => {
let result = await AuthSession.startAsync({ authUrl: url })
this.getTokenbyCode(result.params.code)
};
getTokenbyCode = async (code) => {
const details = {
grant_type: 'authorization_code',
code,
client_id: '10eavoe3ufj2d70m5m3m2hl4pl',
redirect_uri: AuthSession.getRedirectUrl()
}
const formBody = Object.keys(details)
.map(
key => `${encodeURIComponent(key)}=${encodeURIComponent(details[key])}`
)
.join("&");
await fetch(
'https://presentor.auth.us-west-2.amazoncognito.com/oauth2/token',
{
method: "POST",
headers: {
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: formBody
}
)
.then(async (res) => {
console.log('res: ', res);
let resJSON = await res.json();
let idToken = await resJSON.id_token;
let decodedToken = jwt(idToken);
let userData = {
Username : decodedToken["cognito:username"],
Pool : Auth.userPool
}
})
.catch(error => {
console.log('error: ', error);
});
}
当我解码令牌时,我看到了预期的负载,但是如果我想使用 API 在令牌过期时刷新它,我必须手动解决(检查过期并检索一个新的令牌过期)。
我错过了一些基本的东西吗?
【问题讨论】:
标签: react-native amazon-cognito aws-amplify