chatbot-template/app/(auth)/auth.ts
2024-11-15 12:18:17 -05:00

56 lines
1.3 KiB
TypeScript

import { compare } from 'bcrypt-ts';
import NextAuth, { type User, type Session } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { getUser } from '@/lib/db/queries';
import { authConfig } from './auth.config';
interface ExtendedSession extends Session {
user: User;
}
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
...authConfig,
providers: [
Credentials({
credentials: {},
// biome-ignore lint/suspicious/noExplicitAny: TODO
async authorize({ email, password }: any) {
const users = await getUser(email);
if (users.length === 0) return null;
const passwordsMatch = await compare(password, users[0].password!);
// biome-ignore lint/suspicious/noExplicitAny: TODO
return users[0] as any;
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id;
}
return token;
},
async session({
session,
token,
}: {
session: ExtendedSession;
// biome-ignore lint/suspicious/noExplicitAny: TODO
token: any;
}) {
if (session.user) {
session.user.id = token.id as string;
}
return session;
},
},
});