Created
July 24, 2023 06:24
-
-
Save maskaravivek/4cd110a0fa36887ec9d1b4a6e714092c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function refreshAccessToken(token) { | |
| try { | |
| const refreshedTokensResponse = await fetch("https://cognito-idp.us-west-2.amazonaws.com", { | |
| headers: { | |
| "X-Amz-Target": "AWSCognitoIdentityProviderService.InitiateAuth", | |
| "Content-Type": "application/x-amz-json-1.1", | |
| }, | |
| method: "POST", | |
| body: JSON.stringify({ | |
| "AuthFlow": "REFRESH_TOKEN_AUTH", | |
| "ClientId": process.env.COGNITO_CLIENT_ID, | |
| "UserPoolId": process.env.COGNITO_USER_POOL_ID, | |
| "AuthParameters": { | |
| "REFRESH_TOKEN": token.refreshToken, | |
| "SECRET_HASH": process.env.COGNITO_CLIENT_SECRET, | |
| }, | |
| }), | |
| }) | |
| const refreshedTokens = await refreshedTokensResponse.json(); | |
| if (!refreshedTokensResponse.ok) { | |
| throw refreshedTokens; | |
| } | |
| return { | |
| ...token, | |
| accessToken: refreshedTokens.AuthenticationResult.AccessToken, | |
| accessTokenExpires: Date.now() + refreshedTokens.AuthenticationResult.ExpiresIn * 1000, | |
| refreshToken: refreshedTokens.AuthenticationResult.RefreshToken ?? token.refreshToken, // Fall back to old refresh token | |
| }; | |
| } catch (error) { | |
| return { | |
| ...token, | |
| error: "RefreshAccessTokenError", | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment