11import { compare } from 'bcrypt-ts' ;
2- import NextAuth , { type User , type Session } from 'next-auth' ;
2+ import NextAuth , { type DefaultSession } from 'next-auth' ;
33import Credentials from 'next-auth/providers/credentials' ;
4+ import { createGuestUser , getUser } from '@/lib/db/queries' ;
5+ import { authConfig } from './auth.config' ;
6+ import { DUMMY_PASSWORD } from '@/lib/constants' ;
7+ import type { DefaultJWT } from 'next-auth/jwt' ;
48
5- import { getUser } from '@/lib/db/queries ';
9+ export type UserType = 'guest' | 'regular ';
610
7- import { authConfig } from './auth.config' ;
11+ declare module 'next-auth' {
12+ interface Session extends DefaultSession {
13+ user : {
14+ id : string ;
15+ type : UserType ;
16+ } & DefaultSession [ 'user' ] ;
17+ }
18+
19+ interface User {
20+ id ?: string ;
21+ email ?: string | null ;
22+ type : UserType ;
23+ }
24+ }
825
9- interface ExtendedSession extends Session {
10- user : User ;
26+ declare module 'next-auth/jwt' {
27+ interface JWT extends DefaultJWT {
28+ id : string ;
29+ type : UserType ;
30+ }
1131}
1232
1333export const {
@@ -22,31 +42,48 @@ export const {
2242 credentials : { } ,
2343 async authorize ( { email, password } : any ) {
2444 const users = await getUser ( email ) ;
25- if ( users . length === 0 ) return null ;
26- // biome-ignore lint: Forbidden non-null assertion.
27- const passwordsMatch = await compare ( password , users [ 0 ] . password ! ) ;
45+
46+ if ( users . length === 0 ) {
47+ await compare ( password , DUMMY_PASSWORD ) ;
48+ return null ;
49+ }
50+
51+ const [ user ] = users ;
52+
53+ if ( ! user . password ) {
54+ await compare ( password , DUMMY_PASSWORD ) ;
55+ return null ;
56+ }
57+
58+ const passwordsMatch = await compare ( password , user . password ) ;
59+
2860 if ( ! passwordsMatch ) return null ;
29- return users [ 0 ] as any ;
61+
62+ return { ...user , type : 'regular' } ;
63+ } ,
64+ } ) ,
65+ Credentials ( {
66+ id : 'guest' ,
67+ credentials : { } ,
68+ async authorize ( ) {
69+ const [ guestUser ] = await createGuestUser ( ) ;
70+ return { ...guestUser , type : 'guest' } ;
3071 } ,
3172 } ) ,
3273 ] ,
3374 callbacks : {
3475 async jwt ( { token, user } ) {
3576 if ( user ) {
36- token . id = user . id ;
77+ token . id = user . id as string ;
78+ token . type = user . type ;
3779 }
3880
3981 return token ;
4082 } ,
41- async session ( {
42- session,
43- token,
44- } : {
45- session : ExtendedSession ;
46- token : any ;
47- } ) {
83+ async session ( { session, token } ) {
4884 if ( session . user ) {
49- session . user . id = token . id as string ;
85+ session . user . id = token . id ;
86+ session . user . type = token . type ;
5087 }
5188
5289 return session ;
0 commit comments