-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
TypeScript issues in session callback #7132
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
I can confirm a similar regression moving from import type { DefaultSession } from 'next-auth';
declare module 'next-auth' {
interface Session {
user: DefaultSession['user'] & {
id: string;
};
}
} During build I get this error attempting to write to the
Thus it's likely the signature of that callback has a breaking change. Checking the types it shows that the |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
What's funny is that I was running an older version when the problem appeared, something like 4.10, so my first reflex was to update to 4.21.0 😅 Should we stick to 4.20.1 until there's a fix or a documented workaround for 4.21.0? Thanks in advance folks |
Can confirm 4.20.1 works. We've pinned create-t3-app to that version for now. |
Thanks for the heads up. Technically, the incoming next-auth/packages/next-auth/src/core/routes/session.ts Lines 64 to 71 in c0bf2f1
and here: next-auth/packages/next-auth/src/core/routes/session.ts Lines 137 to 142 in c0bf2f1
So actually the typing is more correct now. Only the return type of the session callback is of type Maybe the true problem is that we've been recommending mutating the incoming param instead of creating a new object on return, so from the top of this thread, this: async session({ session, token, user }) {
// Add property to session, like an access_token from a provider.
//console.log("Session callback", session, token, user)
session.user.id = token.sub;
session.user.isAdmin = token.isAdmin;
session.user.vendorId = token.vendorId;
session.user.stripe_id = token.stripeId;
return session
}, should be this: session({ session, token }) {
return { ...session,
user: { ...session.user,
id: token.sub,
isAdmin: token.isAdmin,
vendorId: token.vendorId,
stripe_id: token.stripeId
}
}
}, which is more correct, advertises immutability, and works with both old and new typings. I understand it's breaking the current behavior though, so tagging @ThangHuuVu for thoughts, maybe we should revert after all to be less disruptive, even though it's kind of a small lie in the session param type. Open to feedback and thoughts. NOTE: Please don't comment "same issue" or anything if it has no constructive meaning to the issue. It creates noise. If it's relevant to you, always hit the 👍 on the top post. |
I think this is fair, and I just changed to the object-spread method and it works. If it's the type that's actually passed, I think it should stay and update documentation to show this new way of module augmentation. The problem is it came out of nowhere without any documentation notice in a minor release that this behavior had been changed. |
I want to share two relevant things to keep in mind regarding your mention of creating a new object instead of mutating it. The first one is technical in nature and relates to the developer ergonomics. Since session: ({ session, user }) => {
if (session.user !== undefined) {
session.user.id = user.id;
}
return session;
}, The second point relates to the widespread use of the current approach of handling things. @juliusmarminge already referenced this issue in the PR he posted while I was still drafting this answer, but the create-t3-app is quite popular, being promoted by influencers etc. Many fresh developers with less experience will have picked it up and not necessarily expect such an unfortunate breaking change to begin with which may cause friction in downstream packages so to speak. Hope these thoughts are of value to your decision process on how to move forward with that particular issue :) |
We do state we don't consider type changes breaking https://next-auth.js.org/getting-started/typescript#contributing But I guess this take does not age well, so I'll stop doing it from now on... Sorry for the inconvenience... 🙏 💚 Regarding: session: ({ session, user }) => {
if (session.user !== undefined) {
session.user.id = user.id;
}
return session;
} This would never evaluate to false in Anyway, revert it is, then! We can correct this in |
I pushed the fix, it's released in |
Thanks a lot for the quick fix @balazsorban44, it works well. I've noticed also that isNewUser is becoming deprecated in the jwt fallback. I know it should be a separate issue, but sometimes for makers like I am, we're no professional devs and keeping up with breaking changes in libraries we use is pretty hard. I can only imagine how hard it is at your end to make a library like this live, but I believe sometimes having an updated documentation and more complete release notes help us locate if there's an issue with a new version. Perhaps what I'm saying is non sense, but I definitely value the clarity of the documentation, which is BTW one of the asset of next-auth. Anyway, if you need a hand documenting a change or another, I can try. Otherwise, keep on the good work, thanks a lot for this amazing library. |
New docs will be driven by source code so it won't be possible to make it outdated (besides manual guides etc.), a lot of effort is being put into this. 🙏 We are considering every breaking changes with Thang and see if we can rather do them in 1 or 2 major later down the line to give enough time for migration. |
I'm using the sveltekit wrapper for this. How would I get these changes into that? |
Also using SvelteKit wrapper and still seeing these errors. |
I have 4.22.1 and this is still happening |
Just wanted to confirm that I am also seeing this happen in 4.22.1 |
Also dealing with this in SvelteKit. Here is my current workaround using type imports from the module augmentation:
import type { Session, User } from "@auth/core/types"; session(params) {
const session: Session = params.session;
const user: User = params.user;
if (session.user !== undefined) {
session.user.permissions = user.permissions;
}
return session;
}
import { DefaultSession } from '@auth/core/types';
declare module '@auth/core/types' {
interface Session {
user?: {
id?: string;
permissions?: string[];
} & DefaultSession['user'];
}
interface User {
permissions?: string[];
}
} |
Wondering if there's any progress on this. I'm running into this same issue on version 4.22.3 |
4.23.1 and this still occurs |
any workaround for this? |
haven't figured out anything as well -- 4.23.1 |
using version 4.24.1 and this worked for me |
having the same issue here |
changing to version 5.0.0-beta.4 actually worked for me |
` TRIED THIS WORKAROUND →
` |
|
travistylervii had found a solution by downgrading to beta.4. Be carefull: the ^symbol must deleted. In my case, i did: yarn upgrade [email protected]. (issue 9633) |
it depends on the shape of your user type if extended to add/carry token field |
thanks this worked for me, package.json auth.config.ts |
|
I am using PostgresAdapter, I got this far, but only getting an error on Adapter User
|
Thank you so much!! |
Same issue, in both the current and beta.
|
Environment
System:
OS: macOS 13.1
CPU: (8) arm64 Apple M2
Memory: 604.00 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 19.1.0 - /usr/local/bin/node
Yarn: 3.3.0 - /usr/local/bin/yarn
npm: 8.19.3 - /usr/local/bin/npm
Browsers:
Safari: 16.2
Reproduction URL
https://github.com/lionelrudaz/tasters/tree/release
Describe the issue
I've not done any change on my environment lately, but I have a build error with the latest version of Next.
Here it is:
This is in the session callback method.
Here's the complete method:
This is my next-auth.d.ts file:
Nothing really different from the documentation.
I've checked my code, looks like session is from type DefaultSession and not Session anymore. I have no clue why.
I've tried to implement a DefaultSession interface, no luck...
My file was in /interfaces/next-auth.d.ts, I've moved it in /types like in https://next-auth.js.org/getting-started/typescript, no luck.
Here's my tsconfig.js file:
I've checked the docs over and over, there must be something silly that has changed in a recent version, I just don't get it. It looks like it's a different issue than #7033 encountered by @krinklesaurus and @arnekolja.
What I understand is that now in the session() callback, the type is DefaultSession instead of Session. Which means my interface is not used anymore. No idea why it stopped working all the sudden.
Let me know if there's anything useful I can add.
How to reproduce
Explained above.
Expected behavior
Extend the session with additional attributes.
The text was updated successfully, but these errors were encountered: