DEV Community

Alessandro Prencipe
Alessandro Prencipe

Posted on

AWS Amplify how to set user session manually

Here's how I solved this problem using amazon-cognito-identity-js

const userPool = new CognitoUserPool({
UserPoolId: 'userPoolId',
ClientId: 'userPoolWebClientId',
});

const cognitoIdToken = new CognitoIdToken({
  IdToken: idToken,
});
const cognitoAccessToken = new CognitoAccessToken({
  AccessToken: accessToken,
});
const cognitoRefreshToken = new CognitoRefreshToken({
  RefreshToken: refreshToken,
});
const username = (cognitoIdToken.payload as any).email;

const user = new CognitoUser({
  Username: username,
  Pool: userPool,
});
user.setSignInUserSession(
  new CognitoUserSession({
    AccessToken: cognitoAccessToken,
    IdToken: cognitoIdToken,
    RefreshToken: cognitoRefreshToken,
  })
);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)