Client session and server Session are different
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
Hi
I'm using Next-Auth and I'm using email provider.
I have modified the return value of session callback to return the user id and some other fake data. I have access to added field on client use using
So modified session are available on client, but not on the server.
How can I solve this issue?
Thanks in advance
I'm using Next-Auth and I'm using email provider.
I have modified the return value of session callback to return the user id and some other fake data. I have access to added field on client use using
useSession hook. but when I read the session from server using getServerSession(authOption) I don't have access to those values. So modified session are available on client, but not on the server.
How can I solve this issue?
Thanks in advance
6 Replies
@Spectacled bear Hi
I'm using Next-Auth and I'm using email provider.
I have modified the return value of session callback to return the user id and some other fake data. I have access to added field on client use using `useSession` hook. but when I read the session from server using `getServerSession(authOption)` I don't have access to those values.
So modified session are available on client, but not on the server.
How can I solve this issue?
Thanks in advance
How do you modify the session? Can you show your
Generally, passing the auth options to
authOptions?Generally, passing the auth options to
getServerSession should work as expected@fuma How do you modify the session? Can you show your `authOptions`?
Generally, passing the auth options to `getServerSession` should work as expected
Spectacled bearOP
This is my authOptions
const authOption = NextAuth({
pages: {
verifyRequest: "/verify",
signIn: "/login",
},
session: { strategy: "jwt" },
adapter: MongoDBAdapter(clientPromise),
callbacks: {
redirect(params) {
switch (params.url) {
case process.env.NEXTAUTH_URL:
return undefined;
default:
return params.url;
}
},
async jwt({ token, account, profile }) {
// Persist the OAuth access_token and or the user id to the token right after signin
token.id = 'hello-world'
return token;
},
async session({ session, token, user }) {
session.user.name = 'javad'
session.user.customID = 'hello-world'
session.user.id = 'id field'
// Send properties to the client, like an access_token and user id from a provider.
return session;
},
},
providers: [
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
sendVerificationRequest,
}),
],
});Dwarf Crocodile
https://next-auth.js.org/configuration/callbacks
danger
The session object is not persisted server side, even when using database sessions - only data such as the session token, the user, and the expiry time is stored in the session table.
If you need to persist session data server side, you can use the accessToken returned for the session as a key - and connect to the database in the session() callback to access it. Session accessToken values do not rotate and are valid as long as the session is valid.
If using JSON Web Tokens instead of database sessions, you should use the User ID or a unique key stored in the token (you will need to generate a key for this yourself on sign in, as access tokens for sessions are not generated when using JSON Web Tokens).Since you're using the JWT strategy, if you want your custom fields to persist in both client and server, you might want to add it in the JWT callback
(though note you would still keep the session callback since "If you want to make something available you added to the token (like access_token and user.id from above) via the jwt() callback, you have to explicitly forward it here to make it available to the client.")
(though note you would still keep the session callback since "If you want to make something available you added to the token (like access_token and user.id from above) via the jwt() callback, you have to explicitly forward it here to make it available to the client.")
@Spectacled bear
It’s allowed If you want to access the user id from a session, but since you hid some lines of code, I can’t sure where is the problem.
There is an example worked in existing projects:
For other properties, as Jeremy mentioned. You have to forward it from the jwt callback.
There is an example worked in existing projects:
callbacks: {
session: async ({ session, token }) => {
if (session.user != null) {
session.user.id = token.uid;
}
return session;
},
jwt: async ({ user, token }) => {
if (user != null) {
token.uid = user.id;
}
return token;
},
},For other properties, as Jeremy mentioned. You have to forward it from the jwt callback.