@@ -7,13 +7,7 @@ import { ServerAction } from 'common/actions'
77import { logger } from '@/util/logger'
88import { triggerMonthlyResetAndGrant } from '@codebuff/billing'
99
10- // List of admin user emails
11- const ADMIN_USER_EMAILS = [
12- 'venkateshrameshkumar+1@gmail.com' ,
13- 'brandonchenjiacheng@gmail.com' ,
14- 'jahooma@gmail.com' ,
15- 'charleslien97@gmail.com' ,
16- ]
10+ import * as internal from '@codebuff/internal'
1711
1812export const checkAuth = async ( {
1913 fingerprintId,
@@ -24,44 +18,31 @@ export const checkAuth = async ({
2418 authToken ?: string
2519 clientSessionId : string
2620} ) : Promise < void | ServerAction > => {
27- if ( ! authToken ) {
28- if ( ! fingerprintId ) {
29- logger . error (
30- { clientSessionId } ,
31- 'Auth token and fingerprint ID are missing'
32- )
33- return {
34- type : 'action-error' ,
35- message : 'Auth token and fingerprint ID are missing' ,
36- }
37- }
38- return
39- }
40-
41- const user = await db
42- . select ( {
43- id : schema . user . id ,
44- email : schema . user . email ,
45- discord_id : schema . user . discord_id ,
46- } )
47- . from ( schema . user )
48- . innerJoin ( schema . session , eq ( schema . user . id , schema . session . userId ) )
49- . where ( eq ( schema . session . sessionToken , authToken ) )
50- . then ( ( users ) => {
51- if ( users . length === 1 ) {
52- return users [ 0 ]
53- }
54- return undefined
55- } )
21+ // Use shared auth check functionality
22+ const authResult = await internal . utils . checkAuthToken ( {
23+ fingerprintId,
24+ authToken,
25+ } )
5626
57- if ( ! user ) {
58- logger . warn ( { clientSessionId } , 'Invalid auth token' )
27+ if ( ! authResult . success ) {
28+ logger . error (
29+ { clientSessionId, error : authResult . error } ,
30+ authResult . error ?. message || 'Authentication failed'
31+ )
5932 return {
6033 type : 'action-error' ,
61- message : 'Invalid auth token ',
34+ message : authResult . error ?. message || 'Authentication failed ',
6235 }
6336 }
6437
38+ if ( authResult . user ) {
39+ // Log successful authentication if we have a user
40+ logger . debug (
41+ { clientSessionId, userId : authResult . user . id } ,
42+ 'Authentication successful'
43+ )
44+ }
45+
6546 return
6647}
6748
@@ -98,7 +79,7 @@ export const checkAdmin = async (
9879 return res . status ( 401 ) . json ( { error : errorMessage } )
9980 }
10081
101- // Get the user email associated with this session token
82+ // Get the user ID associated with this session token
10283 const user = await db
10384 . select ( {
10485 id : schema . user . id ,
@@ -109,17 +90,22 @@ export const checkAdmin = async (
10990 . where ( eq ( schema . session . sessionToken , authToken ) )
11091 . then ( ( users ) => users [ 0 ] )
11192
112- // Check if user has admin access
113- if ( ! user ?. email || ! ADMIN_USER_EMAILS . includes ( user . email ) ) {
93+ if ( ! user ) {
94+ return res . status ( 401 ) . json ( { error : 'Invalid session' } )
95+ }
96+
97+ // Check if user has admin access using shared utility
98+ const adminUser = await internal . utils . checkUserIsCodebuffAdmin ( user . id )
99+ if ( ! adminUser ) {
114100 logger . warn (
115- { userId : user ? .id , email : user ? .email , clientSessionId } ,
101+ { userId : user . id , email : user . email , clientSessionId } ,
116102 'Unauthorized access attempt to admin endpoint'
117103 )
118104 return res . status ( 403 ) . json ( { error : 'Forbidden' } )
119105 }
120106
121107 // Store user info in request for handlers to use if needed
122- // req.user = user // TODO: ensure type check passes
108+ // req.user = adminUser // TODO: ensure type check passes
123109
124110 // Auth passed and user is admin, proceed to next middleware
125111 next ( )
0 commit comments