Currently, I am utilizing Next.js on the client side, auth0 for authentication, and Django Rest Framework for the backend. By following Auth0's Manage Metadata Using the Management API guide, I successfully managed to set new metadata values (verified from the Dashboard). However, upon refreshing the user's profile page (pages/profile.js), the old session metadata persists.
My query pertains to updating the user session post setting the metadata values. I attempted to utilize updateSession from @auth/nextjs-auth as well as explored other options suggested in nextjs-auth0: update user session (without logging out/in) after updating user_metadata. Unfortunately, I encountered issues with checkSession() not being defined which left me confused.
index.js
async function handleAddToFavourite() {
if (selected) {
const data = await axios.patch("api/updateUserSession",)
// Update the user session for the client side
checkSession() //this function is not defined
}
}
api/updateUserSession.js
async function handler(req, res) {
const session = await getSession(req, res);
if (!session || session === undefined || session === null) {
return res.status(401).end();
}
const id = session?.user?.sub;
const { accessToken } = session;
const currentUserManagementClient = new ManagementClient({
token: accessToken,
domain: auth0_domain.replace('https://', ''),
scope: process.env.AUTH0_SCOPE,
});
const user = await currentUserManagementClient.updateUserMetadata({ id }, req.body);
await updateSession(req, res, { ...session, user }); // Add this to update the session
return res.status(200).json(user);
}
export default withApiAuthRequired(handler);
api/auth/[...auth0].js
const afterRefetch = (req, res, session) => {
const newSession = getSession(req, res)
if (newSession) {
return newSession as Promise<Session>
}
return session
}
export default handleAuth({
async profile(req, res) {
try {
await handleProfile(req, res, {
refetch: true,
afterRefetch
});
} catch (error: any) {
res.status(error.status || 500).end(error.message);
}
},
});
Despite my efforts, I find myself needing to log out and back in to observe the updated metadata values. Is there a way to update the user's session via Django Rest Framework after modifying the metadata? If not, how can I achieve this using updateSession in Node.js?
Thank you for dedicating your time to this matter.