How to refresh the access token in aws amplify from front end react app ?

0

We have a react based front end app. It uses amplify in front end to interact with cognito. We use hosted cognito login page in our react web app. When we send the access token to backend api backed by API GW which uses cognito to authorize and authenticate. The issue is sometime the access is getting expired. It looks like the access token is available for 1 hour only.

How do we know whether the token is valid or not in front end code using aws amplify ? If it is expired, how do we use amplify sdk/api to refresh and get the new token without refreshing the page ?

Note:

  • When we manually refresh the page, it is working.
  • We want to perform the api call make it as a successful api call without asking the user to manually refresh the page.
1回答
1
承認された回答

Hi Navin,

You should just call Auth.currentSession() method, as Amplify will handle that, see this: https://docs.amplify.aws/lib/auth/manageusers/q/platform/js/#retrieve-current-session

Hope it helps and appreciated an accepted answer if so, so that benefits community when searching for similar issues, cheers!

profile picture
エキスパート
回答済み 1年前
profile picture
エキスパート
レビュー済み 1ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ