Next-auth problem with type with the adapter
Unanswered
Brouillard posted this in #help-forum
BrouillardOP
here's my auth file :
I added types to my session but the prisma adapter gives me a type error
import type {
GetServerSidePropsContext,
NextApiRequest,
NextApiResponse,
} from "next"
import type { NextAuthOptions } from "next-auth"
import { getServerSession } from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import EmailProvider from "next-auth/providers/email"
import { PrismaAdapter } from "@auth/prisma-adapter"
import { PrismaClient } from "@prisma/client"
const prisma = new PrismaClient()
export const config = {
pages: {
signIn: "/login",
signOut: "/auth/signout",
},
adapter: PrismaAdapter(prisma),
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {},
async authorize(credentials: any): Promise<any> {
const user = JSON.parse(credentials.user)
},
}),
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: Number(process.env.EMAIL_SERVER_PORT),
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
],
callbacks: {
jwt: async ({ token, user }) => {
user && (token.user = user)
return token
},
session: async ({ session, token }) => {
if (token?.user && session?.user) {
session.user.role = token.user.role
}
return session
}
},
secret: process.env.NEXTAUTH_SECRET,
} satisfies NextAuthOptions
// Use it in server contexts
export function auth(
...args:
| [GetServerSidePropsContext["req"], GetServerSidePropsContext["res"]]
| [NextApiRequest, NextApiResponse]
| []
) {
return getServerSession(...args, config)
}
I added types to my session but the prisma adapter gives me a type error
2 Replies
BrouillardOP
import NextAuth, { DefaultSession } from "next-auth"
import { JWT } from "next-auth/jwt"
import { AdapterUser } from "next-auth/adapters"
declare module "next-auth" {
interface User {
role: string
}
interface Session {
user: {
role: string
} & DefaultSession["user"]
}
}
declare module "next-auth/jwt" {
interface JWT {
user: {
role: string
} & DefaultSession["user"]
}
}
And here's my next-auth.ts
BrouillardOP
help