File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { NextRequest , NextResponse } from 'next/server'
2+
3+ import { apiCookieHeaders } from '@/utils/apiCookieHeaders'
4+ import { ssrApiClient } from '@/utils/queryClient'
5+
6+ export async function middleware ( request : NextRequest ) {
7+ const { pathname } = request . nextUrl
8+
9+ // Check if the request is for /mega or any sub-path under /mega
10+ if ( pathname . startsWith ( '/mega' ) ) {
11+ try {
12+ // Parse cookies from request
13+ const cookies = Object . fromEntries ( request . cookies . getAll ( ) . map ( ( cookie ) => [ cookie . name , cookie . value ] ) )
14+ const headers = apiCookieHeaders ( cookies )
15+
16+ // Get user's organizations
17+ const organizations = await ssrApiClient . organizationMemberships
18+ . getOrganizationMemberships ( )
19+ . request ( { headers } )
20+ . then ( ( res ) => res . map ( ( m ) => m . organization ) . filter ( ( o ) => o !== null ) )
21+
22+ // Auto-join mega if user has no organizations
23+ if ( organizations . length === 0 ) {
24+ await ssrApiClient . organizations . postJoinByToken ( ) . request ( 'mega' , 's3AX1iyAx3sgGNygiM67' , { headers } )
25+ }
26+ } catch ( error ) {
27+ // Ignore errors during auto-join
28+ console . error ( 'Auto-join mega failed:' , error )
29+ }
30+ }
31+
32+ return NextResponse . next ( )
33+ }
34+
35+ export const config = {
36+ matcher : '/mega/:path*'
37+ }
You can’t perform that action at this time.
0 commit comments