Initializing Cognito User Session with OAuth Tokens Using amazon-cognito-identity-js

18 Views Asked by At

Is there a way to use the amazon-cognito-identity-js library to interact with a Cognito User Pool, initializing the user session directly from OAuth 2.0 tokens (ID token, access token, and refresh token) obtained via the authorization code grant flow from the token endpoint?

I have this question because amazon-cognito-identity-js abstracts away a lot of functionality, and it would be great to be able to simply "plug and play" once I receive the code from the Cognito HostedUI. I thought the library might even have a way to directly extract the tokens given the code value, but I couldn't find a way to do so and decided to directly call the token endpoint for this first interaction.

It would be great if I could just call the userPool object I created here and give it the tokens so that it handles storage and refreshing and gives me some pre-built API calls for the authenticated user.

import { CognitoUserPool } from 'amazon-cognito-identity-js';

const poolData = {
  UserPoolId: process.env.NEXT_PUBLIC_USER_POOL_ID,
  ClientId: process.env.NEXT_PUBLIC_CLIENT_ID,
};

export const userPool = new CognitoUserPool(poolData);

This would be of great help. I'm sorry for the basic question, but I can't find this anywhere and have a feeling it has to be possible somehow without building everything from scratch.

0

There are 0 best solutions below