1- import * as bcrypt from 'bcrypt'
21import NextAuth from 'next-auth'
32import CredentialsProvider from 'next-auth/providers/credentials'
4- import GithubProvider from 'next-auth/providers/github '
3+ import * as bcrypt from 'bcrypt '
54
6- import { getConfig } from '@/lib/config'
75import { getDatabase } from '@/lib/database'
8- import {
9- StorageAdapter ,
10- userFromAccount
11- } from '@/lib/services/auth/storageAdapter'
12- import { headerHost } from '@/lib/services/guards/headerHost'
13-
14- const getAuthConfig = ( ) => {
15- try {
16- const { secretPhase, auth, serviceName } = getConfig ( )
17-
18- if ( ! secretPhase ) {
19- console . error ( 'Secret phase is not configured' )
20- throw new Error ( 'Secret phase is not configured' )
21- }
22-
23- const providers = [
24- CredentialsProvider ( {
25- name : serviceName ?? 'credentials' ,
26- credentials : {
27- actorId : { label : 'Actor Address' , type : 'text' } ,
28- password : { label : 'Password' , type : 'password' }
29- } ,
30- async authorize ( credentials , request ) {
31- try {
32- const hostname = headerHost ( request . headers )
33- if ( ! credentials ) return null
34-
35- const database = getDatabase ( )
36- if ( ! database ) return null
37-
38- const { actorId, password } = credentials
39- const [ username , domain ] = ( actorId as string ) . split ( '@' )
40- const actor = await database . getActorFromUsername ( {
41- username,
42- domain : domain ?? hostname
43- } )
44- if ( ! actor ) return null
456
46- const account = actor . account
47- if ( ! account ?. passwordHash ) return null
48- if ( ! account . verifiedAt ) return null
7+ export const { auth, handlers, signIn, signOut } = NextAuth ( {
8+ trustHost : true ,
9+ session : {
10+ strategy : 'jwt'
11+ } ,
12+ providers : [
13+ CredentialsProvider ( {
14+ name : 'credentials' ,
15+ credentials : {
16+ actorId : { label : 'Actor Address' , type : 'text' } ,
17+ password : { label : 'Password' , type : 'password' }
18+ } ,
19+ async authorize ( credentials , request ) {
20+ if ( ! credentials ?. actorId || ! credentials ?. password ) {
21+ return null
22+ }
4923
50- const isPasswordCorrect = await bcrypt . compare (
51- password as string ,
52- account . passwordHash
53- )
24+ const database = getDatabase ( )
25+ if ( ! database ) return null
5426
55- if ( ! isPasswordCorrect ) return null
56- return userFromAccount ( account )
57- } catch ( error ) {
58- console . error ( 'Authorization error:' , error )
27+ try {
28+ const actor = await database . getActorFromEmail ( {
29+ email : credentials . actorId
30+ } )
31+
32+ if ( ! actor ?. account ?. passwordHash ) {
5933 return null
6034 }
61- }
62- } )
63- ] as any [ ]
64-
65- // Only add GitHub provider if configured
66- if ( auth ?. github ?. id && auth ?. github ?. secret ) {
67- providers . push (
68- GithubProvider ( {
69- clientId : auth . github . id ,
70- clientSecret : auth . github . secret
71- } )
72- )
73- }
7435
75- const config = {
76- trustHost : true ,
77- session : {
78- strategy : 'database' as const
79- } ,
80- providers,
81- pages : {
82- signIn : '/auth/signin'
83- } ,
84- callbacks : {
85- async signIn ( { user } : { user : any } ) {
86- try {
87- const database = getDatabase ( )
88- if ( ! database ) return false
89-
90- const account = await database . getAccountFromId ( { id : user . id } )
91- if ( ! account ?. verifiedAt ) return false
36+ const isValid = await bcrypt . compare (
37+ credentials . password ,
38+ actor . account . passwordHash
39+ )
40+
41+ if ( ! isValid ) return null
9242
93- return true
94- } catch ( error ) {
95- console . error ( 'SignIn callback error:' , error )
96- return false
43+ return {
44+ id : actor . account . id ,
45+ email : actor . account . email ,
46+ name : actor . name || actor . username ,
47+ image : actor . avatar
9748 }
49+ } catch ( error ) {
50+ console . error ( 'Authentication error:' , error )
51+ return null
9852 }
99- } ,
100- adapter : StorageAdapter ( secretPhase )
101- }
102-
103- return config
104- } catch ( error ) {
105- console . error ( 'Auth config error:' , error )
106- return {
107- providers : [
108- CredentialsProvider ( {
109- name : 'credentials' ,
110- credentials : {
111- actorId : { label : 'Actor Address' , type : 'text' } ,
112- password : { label : 'Password' , type : 'password' }
113- } ,
114- async authorize ( ) {
115- return null
116- }
117- } )
118- ] ,
119- session : {
120- strategy : 'database' as const
121- } ,
122- trustHost : true
123- }
53+ }
54+ } )
55+ ] ,
56+ pages : {
57+ signIn : '/auth/signin'
12458 }
125- }
59+ } )
12660
127- export const { auth, handlers, signIn, signOut } = NextAuth ( getAuthConfig ( ) )
61+ // Export providers for server-side use (serialize to remove functions)
62+ export const getServerProviders = ( ) => [
63+ {
64+ id : 'credentials' ,
65+ name : 'credentials' ,
66+ type : 'credentials'
67+ }
68+ ]
0 commit comments