11import * as bcrypt from 'bcrypt'
2- import { memoize } from 'lodash'
32import NextAuth from 'next-auth'
43import CredentialsProvider from 'next-auth/providers/credentials'
54import GithubProvider from 'next-auth/providers/github'
@@ -12,29 +11,33 @@ import {
1211} from '@/lib/services/auth/storageAdapter'
1312import { headerHost } from '@/lib/services/guards/headerHost'
1413
15- export const getAuthConfig = memoize ( ( ) => {
14+ const getAuthConfig = ( ) => {
1615 try {
1716 const { secretPhase, auth, serviceName } = getConfig ( )
18- return {
19- trustHost : true ,
20- session : {
21- strategy : 'database' as const
22- } ,
23- 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 ) {
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 {
3132 const hostname = headerHost ( request . headers )
3233 if ( ! credentials ) return null
3334
3435 const database = getDatabase ( )
36+ if ( ! database ) return null
37+
3538 const { actorId, password } = credentials
3639 const [ username , domain ] = ( actorId as string ) . split ( '@' )
37- const actor = await database ? .getActorFromUsername ( {
40+ const actor = await database . getActorFromUsername ( {
3841 username,
3942 domain : domain ?? hostname
4043 } )
@@ -51,38 +54,74 @@ export const getAuthConfig = memoize(() => {
5154
5255 if ( ! isPasswordCorrect ) return null
5356 return userFromAccount ( account )
57+ } catch ( error ) {
58+ console . error ( 'Authorization error:' , error )
59+ return null
5460 }
55- } ) ,
61+ }
62+ } )
63+ ] as any [ ]
64+
65+ // Only add GitHub provider if configured
66+ if ( auth ?. github ?. id && auth ?. github ?. secret ) {
67+ providers . push (
5668 GithubProvider ( {
57- clientId : auth ? .github ? .id || '' ,
58- clientSecret : auth ? .github ? .secret || ''
69+ clientId : auth . github . id ,
70+ clientSecret : auth . github . secret
5971 } )
60- ] ,
72+ )
73+ }
74+
75+ const config = {
76+ trustHost : true ,
77+ session : {
78+ strategy : 'database' as const
79+ } ,
80+ providers,
6181 pages : {
6282 signIn : '/auth/signin'
6383 } ,
6484 callbacks : {
6585 async signIn ( { user } : { user : any } ) {
66- const database = getDatabase ( )
67- if ( ! database ) return false
86+ try {
87+ const database = getDatabase ( )
88+ if ( ! database ) return false
6889
69- const account = await database . getAccountFromId ( { id : user . id } )
70- if ( ! account ?. verifiedAt ) return false
90+ const account = await database . getAccountFromId ( { id : user . id } )
91+ if ( ! account ?. verifiedAt ) return false
7192
72- return true
93+ return true
94+ } catch ( error ) {
95+ console . error ( 'SignIn callback error:' , error )
96+ return false
97+ }
7398 }
7499 } ,
75100 adapter : StorageAdapter ( secretPhase )
76101 }
77- } catch {
102+
103+ return config
104+ } catch ( error ) {
105+ console . error ( 'Auth config error:' , error )
78106 return {
79- providers : [ ] ,
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+ ] ,
80119 session : {
81120 strategy : 'database' as const
82121 } ,
83122 trustHost : true
84123 }
85124 }
86- } )
125+ }
87126
88127export const { auth, handlers, signIn, signOut } = NextAuth ( getAuthConfig ( ) )
0 commit comments