After exploring the topic extensively, I found valuable insights and discussions on this matter at: https://github.com/aws-amplify/amplify-js/issues/2552
If you are looking to incorporate a "remember me" feature with Amplify, there is a straightforward method available that aligns with your inquiry. It's worth noting that the approach I suggest involves using localStorage instead of sessionStorage, a practice cautioned against by OWASP (as seen in these conversations: Can local storage ever be considered secure?, and highlighted here: ).
This solution serves as an efficient option for testing purposes and quickly implementing functionality (although remember to enhance security measures before deployment -- famous last words, right? haha).
Below are the steps:
- Begin by crafting an awsConfig object encompassing all desired configurations, following guidelines documented in amplify docs:
// awsConfig.tsx module:
const awsConfig = {
// REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',
// REQUIRED - Amazon Cognito Region
region: 'XX-XXXX-X',
//...etc...
//storage: window.sessionStorage OR .localStorage <--- LEAVE THIS OUT OF THIS CONFIG
}
export default awsConfig
- Navigate to your App.tsx or App.js file, and within the App function structure, invoke Auth.configure() while passing in the awsConfig object from step #1 above:
// App.tsx module:
import { Auth } from '@aws-amplify/auth'
import awsConfig from './awsConfig'
//...etc...
const App: React.FC = () => {
Auth.configure(awsConfig)
//...etc...
- Incorporate a check within your Login component to determine if the "remember me" checkbox has been selected, then update the call to the Auth.configure() function accordingly by specifying the preferred storage parameter:
//Login.tsx module
import { Auth } from '@aws-amplify/auth'
import awsConfig from './awsConfig'
//...etc...
rememberLogin
? Auth.configure({ ...awsConfig, storage: localStorage })
: Auth.configure({ ...awsConfig, storage: sessionStorage });
//...etc...
try {
const awsUser = await Auth.signIn(username, password);
//...etc...
- No further steps are required after step #3... that concludes the process...