NextAuth V5 session data undefined
Unanswered
Neotropic Cormorant posted this in #help-forum
Neotropic CormorantOP
// auth.ts
import NextAuth, { type DefaultSession } from "next-auth"
import { PrismaAdapter } from "@auth/prisma-adapter"
import { PrismaClient, Role } from "@prisma/client"
import authConfig from "@/auth.config"
const prisma = new PrismaClient()
declare module "next-auth" {
interface User {
last_name: string;
first_name: string;
telegram_id: string;
username?: string;
chat_id: string;
is_bot: Boolean;
phoneIdentifier?: string;
soldeinitial: number;
soldePhone: number;
code?: string;
codeToken?: string;
role: Role;
}
interface Session {
user: {
id: string;
last_name: string;
first_name: string;
telegram_id: string;
username?: string;
chat_id: string;
is_bot: Boolean;
phoneIdentifier?: string;
soldeinitial: number;
soldePhone: number;
code?: string;
codeToken?: string;
role: Role;
} & DefaultSession["user"]
}
}
export const { auth, handlers, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
session: { strategy: "jwt" },
...authConfig,
callbacks: {
async session({ session, user }) {
console.log(session.user.code)
session.user = {
...session.user,
is_bot: true
}
return session
},
}
})1 Reply
Neotropic CormorantOP
auth.config.ts
import { type NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { signInSchema } from "@/schema";
import { prisma } from "@/modules/prisma";
export default {
providers: [
Credentials({
credentials: {
code: {}
},
type: "credentials",
name: "code",
async authorize(credentials) {
try {
let user = null;
const { code } = await signInSchema.parseAsync(credentials)
user = await prisma.user.findFirst({
where: {
code
}
})
if (!user) {
throw new Error("User not found.")
}
return user
} catch (error: any) {
throw new Error(error.message)
}
},
})
]
} satisfies NextAuthConfigHello, when I try to create the login, session.user.code is undefined at each faith and the session does not work, who would have the solution? please
the code is in 2 parts, the second is at the top