Skip to content

Commit f5ce040

Browse files
authored
fixed:Update auto-join logic for Mega organization (#1861)
1 parent 2e70ff6 commit f5ce040

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

moon/apps/web/middleware.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)