Issue with next-auth and custom role field
Unanswered
Cyan posted this in #help-forum
CyanOP
Hello,
I recently upgraded from
Here is what my current setup looks like that worked with
auth.ts
middleware.ts
This will log
db.ts
Does anyone know how I can get my
I recently upgraded from
next-auth@5.0.0-beta.4 and @auth/core@0.18.4 to next-auth@5.0.0-beta.13 and @auth/core@0.27.0 and my custom role field stop working. I'm also currently using @auth/drizzle-adapter@0.7.0.Here is what my current setup looks like that worked with
next-auth@5.0.0-beta.4 and @auth/core@0.18.4:auth.ts
declare module "next-auth" {
interface User {
role: string;
}
}
declare module "@auth/core/types" {
interface Session {
user: InferSelectModel<typeof users>;
}
}
declare module "@auth/core/adapters" {
interface AdapterUser extends InferSelectModel<typeof users> {}
}
export const { handlers: { GET, POST }, auth } = NextAuth({
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
callbacks: {
session: async ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
role: user.role,
},
}),
},
adapter: {
...DrizzleAdapter(db),
async getSessionAndUser(data) {
const sessionAndUsers = await db
.select({
session: sessions,
user: users,
})
.from(sessions)
.where(eq(sessions.sessionToken, data))
.innerJoin(users, eq(users.id, sessions.userId));
return sessionAndUsers[0] ?? null;
},
},
pages: {
signIn: "/auth",
error: "/auth/error",
},
secret: env.NEXTAUTH_SECRET,
});middleware.ts
export default auth(async (request) => {
console.log(request.auth.user.role);
return NextResponse.next();
});This will log
undefined in next-auth@5.0.0-beta.13 but would log the users role in next-auth@5.0.0-beta.4db.ts
export const db = drizzle(connection, { schema: { ...schema }, logger: false });Does anyone know how I can get my
role field to work properly again?6 Replies
CyanOP
bump
Greek Harehound
bump
CyanOP
ah thanks ill remember not to mix the packages. But the thing that fixed it for me was a change in my .env i needed to append
but now I'm having the issue of my error page now redirects to
/api/auth- NEXTAUTH_URL="http://127.0.0.1:3000"
+ NEXTAUTH_URL="http://127.0.0.1:3000/api/auth"but now I'm having the issue of my error page now redirects to
http://127.0.0.1:3000/api/auth/auth/error instead of http://127.0.0.1:3000/auth/error any clue how to fix this?@Greek Harehound Did u try adding the error page in pages object?
https://next-auth.js.org/configuration/pages
CyanOP
Yes I have.
pages: {
signIn: "/auth",
error: "/auth/error",
},