Modify Session Object Value ? #2226
-
Can I modify the session object?
this returns
on getSession({req}); so any way to add custom key-value or custom value on session obj? Saw this: https://next-auth.js.org/configuration/callbacks#session-callback only name, email and image keys are parsed why so ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Check out the JWT callback as well. For increased security/privacy - by default - we only persist a subset of the user data in the JWT token. https://next-auth.js.org/configuration/callbacks#jwt-callback |
Beta Was this translation helpful? Give feedback.
-
Hi @sushilbalami, I struggled a lot with this one too, trying to retrieve the Here's my import NextAuth from "next-auth";
import { signin } from "next-auth/client";
import Providers from "next-auth/providers";
export default NextAuth({
providers: [
Providers.Twitter({
clientId: process.env.TWITTER_KEY,
clientSecret: process.env.TWITTER_SECRET,
profile(profile) {
return {
id: profile.id_str as string,
name: profile.name,
email: profile.email,
image: (profile.profile_image_url_https as string).replace(
/_normal\.(jpg|png|gif)$/,
".$1"
),
screen_name: profile.screen_name,
};
},
}),
],
callbacks: {
async signIn(_userOrProfile, _account, _OAuthProfile) {
return true;
},
async jwt(token, user, _account, _profile, _isNewUser) {
if (user) {
token.screen_name = user.screen_name;
}
return token;
},
async session(session, token) {
session.screen_name = token.screen_name;
return session;
},
},
}); The key was to add the It may be different from your case, since I'm not using a database yet, but I hope this helps! @balazsorban44 Maybe the documentation could be improved to make this easier, especially to newcomers (I'm new to NextJS). Happy to help with a pull-request with some guidance. Best, |
Beta Was this translation helpful? Give feedback.
Hi @sushilbalami,
I struggled a lot with this one too, trying to retrieve the
screen_name
for a Twitter authentication.Here's my
[...nextauth].ts
file, when I finally got it working: