Is this a bad way of setting up next auth v5 callbacks?

honestly, I just want an opinion on this one lol
import { PrismaAdapter } from "@auth/prisma-adapter"
import { type User } from "@prisma/client"
import NextAuth, { type DefaultSession } from "next-auth"

import { getUserById } from "@/lib/data/user"
import authConfig from "@/server/auth.config"
import { db } from "@/server/db"

declare module "next-auth" {
interface Session extends DefaultSession {
user: Omit<User, "password" | "emailVerified">
}
}

export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
adapter: PrismaAdapter(db),
session: {
strategy: "jwt",
},
callbacks: {
session: async ({ session, token }) => {
if (token) session.user = { ...token, ...session.user }

return session
},
jwt: async ({ token, user }) => {
if (!token.sub) return token

const dbUser = await getUserById(token.sub)

if (!dbUser) return token

const userFields = {
id: dbUser.id,
name: dbUser.name,
email: dbUser.email,
image: dbUser.image,
role: dbUser.role,
}

return { ...userFields, ...user }
},
},
...authConfig,
})
import { PrismaAdapter } from "@auth/prisma-adapter"
import { type User } from "@prisma/client"
import NextAuth, { type DefaultSession } from "next-auth"

import { getUserById } from "@/lib/data/user"
import authConfig from "@/server/auth.config"
import { db } from "@/server/db"

declare module "next-auth" {
interface Session extends DefaultSession {
user: Omit<User, "password" | "emailVerified">
}
}

export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
adapter: PrismaAdapter(db),
session: {
strategy: "jwt",
},
callbacks: {
session: async ({ session, token }) => {
if (token) session.user = { ...token, ...session.user }

return session
},
jwt: async ({ token, user }) => {
if (!token.sub) return token

const dbUser = await getUserById(token.sub)

if (!dbUser) return token

const userFields = {
id: dbUser.id,
name: dbUser.name,
email: dbUser.email,
image: dbUser.image,
role: dbUser.role,
}

return { ...userFields, ...user }
},
},
...authConfig,
})
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server