New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with using getServerSession #7533
Comments
@stanko-tomic did you find any solution? I have the same issue. What ever I return from the I really can't get my head around how it works or what I'm missing and why I don't have control over what to encrypt in the jwt. |
Nope just used getSession() couldn't get it to work. Don't know if it's problem with the code or what. |
I did a small reproduction - maybe @stanko-tomic you want to include in your description - so when contributors come across this issue we have a base for conversation. Here's a code sandbox: https://codesandbox.io/p/sandbox/falling-smoke-iyhks1 The goal:
Where to look?
The jwt callback: {
jwt: ({ token, user }) => {
let serializeToken = token;
if (user) {
serializeToken = {
...token,
...user,
jwt: token.jwt,
bla: 'foo',
name: 'Foooooo',
id: '1234567890'
};
}
return serializeToken;
}
} What's the issue here? |
@stanko-tomic , I think I found the issue. Try modifying your jwt callback as so: {
jwt({ user = {}, token }) {
return {
...token,
user
};
},
} After doing this, I was able to get the user from the getServerSession! I hope it helps! |
There is a fix mentioned here for Next 13 #7486 (comment) |
+1 with a similar issue. My custom user properties are not coming across when using |
hi There, next-auth:4.22.1 In a credential provider sceneria, I was able to get the custom user property using getServerSession(authOptions) in the Next.js /app directory server components. I am new to next.auth and next.js. Sharing my code below. Maybe I am missing anything. My custom fields in User table are as below -- dropping "email/password" fields, adding "name/studentNo" fields:
My "app\api\auth[...nextauth]\route.ts"
Then in a server component,
, session object in log is something like below, as expected:
|
@raver your code works, thanks a lot |
@raver @bencehusi both your codes work. Thanks :) |
@raver thanks for your attribution. Code works well. |
thanks @raver |
@raver ty, worked |
thank you so much @raver |
@raver your approach worked. |
I had a similar issue where Brilliant ! |
Passing in the |
@raver your approach worked. |
Environment
Env
Reproduction URL
Private repository
Describe the issue
When using getServerSession with authOptions req and res, the returned session object includes properties name and image, even though they are not defined in the user model or the authentication callbacks.
So when using getServerSession i am expecting the return of
This is the response gotten from useSession().
When using getServerSession with auth options the return i get is
Even though it should return the email role and id as defined in authOptions.
How to reproduce
So these are my authOptions
Expected behavior
The getServerSession should return the object containing email,role,id as defined in the authOptions, but it does not.
First time writing an issue sorry for any errors.
The text was updated successfully, but these errors were encountered: