2025-09-21 12:03:29 +01:00
|
|
|
import { compare } from 'bcrypt-ts';
|
|
|
|
|
import NextAuth, { type DefaultSession } from 'next-auth';
|
|
|
|
|
import Credentials from 'next-auth/providers/credentials';
|
|
|
|
|
import { createGuestUser, getUser } from '@/lib/db/queries';
|
|
|
|
|
import { authConfig } from './auth.config';
|
|
|
|
|
import { DUMMY_PASSWORD } from '@/lib/constants';
|
|
|
|
|
import type { DefaultJWT } from 'next-auth/jwt';
|
2025-04-25 23:40:15 -07:00
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
export type UserType = 'guest' | 'regular';
|
2025-04-25 23:40:15 -07:00
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
declare module 'next-auth' {
|
2025-04-25 23:40:15 -07:00
|
|
|
interface Session extends DefaultSession {
|
|
|
|
|
user: {
|
|
|
|
|
id: string;
|
|
|
|
|
type: UserType;
|
2025-09-21 12:03:29 +01:00
|
|
|
} & DefaultSession['user'];
|
2025-04-25 23:40:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface User {
|
|
|
|
|
id?: string;
|
|
|
|
|
email?: string | null;
|
|
|
|
|
type: UserType;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
declare module 'next-auth/jwt' {
|
2025-04-25 23:40:15 -07:00
|
|
|
interface JWT extends DefaultJWT {
|
|
|
|
|
id: string;
|
|
|
|
|
type: UserType;
|
|
|
|
|
}
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const {
|
|
|
|
|
handlers: { GET, POST },
|
|
|
|
|
auth,
|
|
|
|
|
signIn,
|
|
|
|
|
signOut,
|
|
|
|
|
} = NextAuth({
|
|
|
|
|
...authConfig,
|
|
|
|
|
providers: [
|
|
|
|
|
Credentials({
|
|
|
|
|
credentials: {},
|
|
|
|
|
async authorize({ email, password }: any) {
|
2024-11-15 12:18:17 -05:00
|
|
|
const users = await getUser(email);
|
2025-04-21 10:26:58 -07:00
|
|
|
|
|
|
|
|
if (users.length === 0) {
|
|
|
|
|
await compare(password, DUMMY_PASSWORD);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [user] = users;
|
|
|
|
|
|
|
|
|
|
if (!user.password) {
|
|
|
|
|
await compare(password, DUMMY_PASSWORD);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const passwordsMatch = await compare(password, user.password);
|
|
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
if (!passwordsMatch) return null;
|
2025-04-21 10:26:58 -07:00
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
return { ...user, type: 'regular' };
|
2025-04-25 23:40:15 -07:00
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
Credentials({
|
2025-09-21 12:03:29 +01:00
|
|
|
id: 'guest',
|
2025-04-25 23:40:15 -07:00
|
|
|
credentials: {},
|
|
|
|
|
async authorize() {
|
|
|
|
|
const [guestUser] = await createGuestUser();
|
2025-09-21 12:03:29 +01:00
|
|
|
return { ...guestUser, type: 'guest' };
|
2024-10-11 18:00:22 +05:30
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
callbacks: {
|
2025-09-21 12:03:29 +01:00
|
|
|
async jwt({ token, user }) {
|
2024-10-11 18:00:22 +05:30
|
|
|
if (user) {
|
2025-04-25 23:40:15 -07:00
|
|
|
token.id = user.id as string;
|
|
|
|
|
token.type = user.type;
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
},
|
2025-09-21 12:03:29 +01:00
|
|
|
async session({ session, token }) {
|
2024-10-11 18:00:22 +05:30
|
|
|
if (session.user) {
|
2025-04-25 23:40:15 -07:00
|
|
|
session.user.id = token.id;
|
|
|
|
|
session.user.type = token.type;
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return session;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|