Session user id does not get added to session
My code:
import { PrismaAdapter } from '@auth/prisma-adapter';
import { type DefaultSession, type NextAuthOptions, getServerSession } from 'next-auth';
import type { Adapter } from 'next-auth/adapters';
import DiscordProvider from 'next-auth/providers/discord';
import { env } from 'env';
import { db } from 'server/db';
/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
* object and keep type safety.
*
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
*/
declare module 'next-auth' {
interface Session extends DefaultSession {
user: {
id: string;
// ...other properties
// role: UserRole;
} & DefaultSession['user'];
}
// interface User {
// // ...other properties
// // role: UserRole;
// }
}
/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
*
* @see https://next-auth.js.org/configuration/options
*/
export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt',
},
callbacks: {
session: ({ session, token }) => ({
...session,
user: {
...session.user,
id: token.sub,
},
}),
},
adapter: PrismaAdapter(db) as Adapter,
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
pages: {
signIn: '/auth/login',
},
};
/**
* Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
*
* @see https://next-auth.js.org/configuration/nextjs
*/
export const getServerAuthSession = () => getServerSession(authOptions);
import { PrismaAdapter } from '@auth/prisma-adapter';
import { type DefaultSession, type NextAuthOptions, getServerSession } from 'next-auth';
import type { Adapter } from 'next-auth/adapters';
import DiscordProvider from 'next-auth/providers/discord';
import { env } from 'env';
import { db } from 'server/db';
/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
* object and keep type safety.
*
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
*/
declare module 'next-auth' {
interface Session extends DefaultSession {
user: {
id: string;
// ...other properties
// role: UserRole;
} & DefaultSession['user'];
}
// interface User {
// // ...other properties
// // role: UserRole;
// }
}
/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
*
* @see https://next-auth.js.org/configuration/options
*/
export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt',
},
callbacks: {
session: ({ session, token }) => ({
...session,
user: {
...session.user,
id: token.sub,
},
}),
},
adapter: PrismaAdapter(db) as Adapter,
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
pages: {
signIn: '/auth/login',
},
};
/**
* Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
*
* @see https://next-auth.js.org/configuration/nextjs
*/
export const getServerAuthSession = () => getServerSession(authOptions);
1 Reply
When i log my session inside my server component it does not log the id key.
Session User:
Does anyone have an idea why the user ID is not getting added to the session?
can anyone help?
{
name: 'default01',
email: '*******@gmail.com',
image: 'https://cdn.discordapp.com/avatars/362937922640543755/a6b9c40f7acc80f0d7284c3e955b43ad.png'
}
{
name: 'default01',
email: '*******@gmail.com',
image: 'https://cdn.discordapp.com/avatars/362937922640543755/a6b9c40f7acc80f0d7284c3e955b43ad.png'
}