I’m working on a Next.js application where I’m using next-auth for authentication. I’ve been trying to add an additional variable to the user session to store custom data. I’ve updated the authentication routine accordingly and added the additional variable to the session. However, when I access the session using getServerSession()
, the additional variable doesn’t seem to be available.
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
export default NextAuth({
providers: [
Credentials({
credentials: {
email: { label: "E-Mail Adresse" },
password: { label: "Passwort", type: "password" },
},
authorize: async(credentials) => {
const email = credentials?.email;
const password = credentials?.password;
const response = await fetch(`${process.env.url}/api/users/login`, {
method: "POST",
body: JSON.stringify({
email,
password
})
});
const data = await response.json();
return data;
}
})
],
callbacks: {
async session(session, user) {
session.additionalVariable = 'value';
return session;
},
},
});
And in another file, I’m using getServerSession()
to retrieve the session:
import { getServerSession } from 'next-auth/react';
export async function myFunction() {
const session = await getServerSession();
console.log(session); // "additionalVariable" is missing here
}
Why isn’t the additional variable available when accessing the session? Is there something I’m overlooking or need to additionally do to ensure that the additional variable is correctly added to and retrieved from the session? Any help would be appreciated!