admin管理员组文章数量:1386688
I have a react app and I am using Cognito to handle user's authentication. I need to know how do I make a call to Cognito with the refresh token so that it gives me back a new token?
I looked into all of the examples from Cognito and they didn't work. They are using dependencies that I don't have and they don't clearly list how to get them.
Can someone please help?
I have a react app and I am using Cognito to handle user's authentication. I need to know how do I make a call to Cognito with the refresh token so that it gives me back a new token?
I looked into all of the examples from Cognito and they didn't work. They are using dependencies that I don't have and they don't clearly list how to get them.
Can someone please help?
Share Improve this question asked Sep 24, 2018 at 14:49 ShaunShaun 4715 silver badges13 bronze badges2 Answers
Reset to default 4From the documentation, to exchange a refresh token for an access token, you need to make a POST request to the token endpoint oauth2/token
. See section 'Exchanging a Refresh Token for Tokens' here: https://docs.aws.amazon./cognito/latest/developerguide/token-endpoint.html
You can use the Fetch API to make requests simply. Here are the docs on it: https://developer.mozilla/en-US/docs/Web/API/Fetch_API.
Since token requests involve sending your client id and client secret to AWS cognito, I'd remend not making this request in React directly. If you did do this in React, it would be possible for someone to find your client id and client secret, and make requests that look like they are ing from your application.
Instead you should have React ask your server to make the request and return the response it receives.
Here's an example:
On the server side, you can make a route like /api/aws/tokens/refresh
or something like that, which expects a refreshToken
in the request body. Then the controller for that route can call this method:
async getAccessToken(refreshToken) {
const endpoint = 'https://mydomain.auth.us-east-1.amazoncognito./oauth2/token';
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${btoa(CLIENT_ID:CLIENT_SECRET)}`
},
body: JSON.stringify({
grant_type: 'refresh_token',
client_id: CLIENT_ID,
refresh_token: refreshToken
})
}
const response = await fetch(endpoint, options);
return response;
}
Note that the Authorization uses the method btoa
, which base64 encodes its input.
On the client side, your React app can call your server like this:
async getAccessToken(refreshToken) {
const endpoint = '/api/aws/tokens/refresh';
const options = {
method: 'POST',
body: JSON.stringify({
refreshToken: refreshToken
})
}
const response = await fetch(endpoint, options);
// store the tokens or return them
}
There are other ways to separate the work of your server an client on this problem, and many things to configure to have this all work, but hopefully this is helpful for getting started.
You can write the client from scratch as Henry suggests or you can use a library and you're good to go.
We've been using this package: https://www.npmjs./package/amazon-cognito-identity-js
It worked just fine. If it doesn't work for you due to some mysterious dependencies problem, please elaborate a little bit more about that. Maybe we'll figure that out.
本文标签: javascriptHow to get new token from Cognito from the frontendStack Overflow
版权声明:本文标题:javascript - How to get new token from Cognito from the frontend? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744500338a2609285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论