Skip to content

how do you set credential info(like accessToken,idtoken,refleshToken) into session storage with amplify gen2 and Cognito?

0

Thanks for looking at my question!
Now i am trying to make login function with amplify gen2 and Cognito. then i can do login in but credential info that Cognito provide is embed into local storage by default.
My hope is the info is embed into session storage So i would like to change storage from local storage to session storage to be embed. \

I have tried to find documentation, but have not been able to find any documentation that resolves the above.
If anyone knows of any, I would be very grateful if you could let me know!

2 Answers
0
Accepted Answer

Hi! I could fix the issue. So I will share it.
if you use amplify gen2 amplify library v6, you can use this.
For more information, see the following articles.
https://docs.amplify.aws/vue/build-a-backend/auth/concepts/tokens-and-credentials/#understand-token-management-options

import { cognitoUserPoolsTokenProvider } from 'aws-amplify/auth/cognito';
import { sessionStorage } from 'aws-amplify/utils';

cognitoUserPoolsTokenProvider.setKeyValueStorage(sessionStorage);

answered 2 years ago

EXPERT

reviewed 2 years ago

0

Try to set Cognito credential information (access token, ID token, etc.) in the session storage using Amplify Gen2, you can follow these steps:

Install and configure Amplify CLI and Amplify libraries:
    Ensure you have the Amplify CLI installed and configured for your project.
    Install the necessary Amplify libraries, including @aws-amplify/auth and @aws-amplify/storage.

Configure Amplify with session storage:
    In your application, import the necessary Amplify modules:

import { Amplify, Auth } from 'aws-amplify';

    Configure Amplify to use session storage for storing credentials:

Amplify.configure({
  Auth: {
    storage: {
      type: 'session',
    },
  },
});

This will tell Amplify to use session storage instead of the default local storage for storing Cognito credential information.

Implement the login function:
    In your login function, use the signIn method from the @aws-amplify/auth module to perform the Cognito authentication:

import { Auth } from 'aws-amplify';

async function login(username, password) {
  try {
    const user = await Auth.signIn(username, password);
    // The Cognito credential information will now be stored in session storage
    return user;
  } catch (error) {
    console.error('Error signing in:', error);
    throw error;
  }
}

After a successful login, the Cognito credential information (access token, ID token, etc.) will be stored in the session storage.

Retrieve the credential information:
    To access the stored credential information, you can use the Auth.currentSession() method:

import { Auth } from 'aws-amplify';

async function getCurrentSession() {
  try {
    const session = await Auth.currentSession();
    return session;
  } catch (error) {
    console.error('Error getting current session:', error);
    throw error;
  }
}

This will return the current Cognito session, which includes the access token, ID token, and other relevant information.

answered 2 years ago

  • Thank you for your answer!

    I think your source is using amplify library v5.but Unfortunately I am using amplify library v6 because Amplify gen2 force me use amplify library v6.
    I'm sorry but please could you replace your source to amplify library v6?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.