Reputation: 950
I am working on a Next.js project using AWS Amplify Gen 2 and need to implement a profile switching experience for users. Each user should be able to explore two different "flavors" of the application by switching between profiles.
To achieve this, I want to store a current_profile
parameter in the session so that:
However, I couldn’t find any official documentation on session management in Amplify Gen 2. Since Amplify primarily uses Cognito and JWT-based authentication, there is no built-in session handling mechanism like traditional server-side frameworks.
localStorage
to store current_profile
on the client (works, but not ideal for SSR).current_profile
in an HttpOnly session cookie or a backend store like DynamoDB/Redis, but unsure about the best approach in an Amplify environment.current_profile
parameter securely across client and server requests?Any insights or best practices would be greatly appreciated! 🚀
Upvotes: 0
Views: 25
Reputation: 69
You can add a little tag called current_profile to each user’s profile in Cognito.
//amplify/auth/resource.ts
import { defineAuth } from '@aws-amplify/backend';
export const auth = defineAuth({
loginWith: {
email: true,
},
userAttributes: {
'custom:current_profile': {
dataType: 'String',
mutable: true,
},
},
});
When they pick a flavor, you update that tag. It sticks with them because it’s saved in Cognito, and you can grab it whenever you need it—whether you’re loading the page on the server or messing around in the browser.
Upvotes: 0