Modifying 'Session' in next-auth

Code (I removed imports and comments as they are not required):
declare module "next-auth" {
interface Session {
user: {
id: string;
username: string;
password: string;
};
}

interface User {
id: string;
username: string;
password: string;
}
}

export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
callbacks: {
session({ session, token }) {
console.log("session", session)
console.log("token", token)
if (token && session.user) {
session.user.id = token.id as string;
}

return session;
},
jwt: async ({ token, user }) => {
console.log("user", user)
if (user) {
token.id = user.id;
}

return token;
},
},
// adapter: PrismaAdapter(prisma),
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "Username", type: "text", placeholder: "admin" },
password: { label: "Password", type: "password" }
},
authorize(credentials) {
console.log(credentials)
const {username, password} = credentials
if (username !== "admin" || password !== "admin") {
throw new Error("invalid credentials");
}
const user = {
id: "1234",
username: "admin",
password: "admin",
}
return user
},
}),
],
};

/**
* 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 = (ctx: {
req: GetServerSidePropsContext["req"];
res: GetServerSidePropsContext["res"];
}) => {
return getServerSession(ctx.req, ctx.res, authOptions);
};
declare module "next-auth" {
interface Session {
user: {
id: string;
username: string;
password: string;
};
}

interface User {
id: string;
username: string;
password: string;
}
}

export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
callbacks: {
session({ session, token }) {
console.log("session", session)
console.log("token", token)
if (token && session.user) {
session.user.id = token.id as string;
}

return session;
},
jwt: async ({ token, user }) => {
console.log("user", user)
if (user) {
token.id = user.id;
}

return token;
},
},
// adapter: PrismaAdapter(prisma),
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "Username", type: "text", placeholder: "admin" },
password: { label: "Password", type: "password" }
},
authorize(credentials) {
console.log(credentials)
const {username, password} = credentials
if (username !== "admin" || password !== "admin") {
throw new Error("invalid credentials");
}
const user = {
id: "1234",
username: "admin",
password: "admin",
}
return user
},
}),
],
};

/**
* 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 = (ctx: {
req: GetServerSidePropsContext["req"];
res: GetServerSidePropsContext["res"];
}) => {
return getServerSession(ctx.req, ctx.res, authOptions);
};
3 Replies
z
zOP2y ago
Now thats there, According to the docs:
z
zOP2y ago
But as you can see in the code im not extending the session...
z
zOP2y ago
yet im getting default session shape
Want results from more Discord servers?
Add your server