11import type { Middleware } from "winterspec/middleware"
22import type { CtxErrorFn } from "./with-ctx-error"
3+ import type { DbClient } from "../db/db-client"
34
45export const withSessionAuth : Middleware <
56 {
67 error : CtxErrorFn
7- db : any
8+ db : DbClient
89 } ,
910 {
1011 auth : {
@@ -13,6 +14,13 @@ export const withSessionAuth: Middleware<
1314 personal_org_id : string
1415 github_username : string
1516 session_id : string
17+ orgs : Array < {
18+ org_id : string
19+ name : string
20+ user_permissions : {
21+ can_manage_packages : boolean
22+ }
23+ } >
1624 }
1725 } ,
1826 { }
@@ -22,28 +30,72 @@ export const withSessionAuth: Middleware<
2230 const token = req . headers . get ( "authorization" ) ?. split ( "Bearer " ) ?. [ 1 ]
2331
2432 // Only check database accounts when we're in a Bun test environment
25- if ( process . env . BUN_TEST === "true" && ctx . db ?. accounts ) {
26- const account = ctx . db . accounts . find ( ( acc : any ) => acc . account_id === token )
27-
33+ if ( process . env . BUN_TEST === "true" && ctx . db ?. getState ) {
34+ const state = ctx . db . getState ( )
35+ const account = state . accounts . find ( ( acc : any ) => acc . account_id === token )
2836 if ( account ) {
37+ // Fetch orgs for this account
38+ const orgAccounts = state . orgAccounts . filter (
39+ ( oa : any ) => oa . account_id === account . account_id ,
40+ )
41+
42+ const orgs = orgAccounts . map ( ( oa : any ) => {
43+ const org = state . organizations . find ( ( o : any ) => o . org_id === oa . org_id )
44+ return {
45+ org_id : oa . org_id ,
46+ name :
47+ org ?. org_display_name ||
48+ org ?. org_name ||
49+ org ?. github_handle ||
50+ oa . org_id ,
51+ user_permissions : { can_manage_packages : true } ,
52+ }
53+ } )
54+
2955 ctx . auth = {
3056 type : "session" ,
3157 account_id : account . account_id ,
3258 personal_org_id : account . personal_org_id || `org-${ account . account_id } ` ,
3359 github_username : account . github_username ,
3460 session_id : `session-${ account . account_id } ` ,
61+ orgs :
62+ orgs . length > 0
63+ ? orgs
64+ : [
65+ {
66+ org_id :
67+ account . personal_org_id || `org-${ account . account_id } ` ,
68+ name :
69+ account . github_username ||
70+ account . personal_org_id ||
71+ `org-${ account . account_id } ` ,
72+ user_permissions : { can_manage_packages : true } ,
73+ } ,
74+ ] ,
3575 }
3676 return next ( req , ctx )
3777 }
3878 }
3979
40- // For all other environments or if account not found in test, use hardcoded auth
80+ // Fallback auth for non-test environments or when no token is found
81+ const fallbackAccountId = "account-1234"
82+ const fallbackOrgId = "org-1234"
83+
84+ const fallbackOrgs = [
85+ {
86+ org_id : fallbackOrgId ,
87+ name : "org-1234" ,
88+ user_permissions : { can_manage_packages : true } ,
89+ } ,
90+ ]
91+
4192 ctx . auth = {
4293 type : "session" ,
43- account_id : "account-1234" ,
44- personal_org_id : "org-1234" ,
94+ account_id : fallbackAccountId ,
95+ personal_org_id : fallbackOrgId ,
4596 github_username : "testuser" ,
4697 session_id : "session-1234" ,
98+ orgs : fallbackOrgs ,
4799 }
48100
49101 return next ( req , ctx )
0 commit comments