2 Answers
- Newest
- Most votes
- Most comments
0
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);
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?