Files
dndextended/src/lib/auth.ts
T
Alexander Harding a3ea76fdff Vibecode central
2026-07-31 19:50:55 -04:00

53 lines
1.4 KiB
TypeScript

import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { compare } from "bcryptjs";
import clientPromise from "./mongodb";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const client = await clientPromise;
const db = client.db("dndextended");
const user = await db.collection("users").findOne({ email: credentials.email });
if (!user) return null;
const valid = await compare(credentials.password as string, user.password);
if (!valid) return null;
return {
id: user._id.toString(),
name: user.name,
email: user.email,
isAdmin: user.isAdmin === true,
};
},
}),
],
session: { strategy: "jwt" },
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id;
token.isAdmin = (user as { isAdmin?: boolean }).isAdmin === true;
}
return token;
},
async session({ session, token }) {
if (token && session.user) {
session.user.id = token.id as string;
session.user.isAdmin = token.isAdmin === true;
}
return session;
},
},
pages: {
signIn: "/login",
},
});