@@ -14,8 +14,8 @@ import { WorkspaceInvitationEmail } from '@/components/emails/workspace-invitati
1414import { getSession } from '@/lib/auth'
1515import { sendEmail } from '@/lib/email/mailer'
1616import { getFromEmailAddress } from '@/lib/email/utils'
17- import { env } from '@/lib/env'
1817import { hasWorkspaceAdminAccess } from '@/lib/permissions/utils'
18+ import { getBaseUrl } from '@/lib/urls/utils'
1919
2020// GET /api/workspaces/invitations/[invitationId] - Get invitation details OR accept via token
2121export async function GET (
@@ -30,12 +30,7 @@ export async function GET(
3030 if ( ! session ?. user ?. id ) {
3131 // For token-based acceptance flows, redirect to login
3232 if ( isAcceptFlow ) {
33- return NextResponse . redirect (
34- new URL (
35- `/invite/${ invitationId } ?token=${ token } ` ,
36- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
37- )
38- )
33+ return NextResponse . redirect ( new URL ( `/invite/${ invitationId } ?token=${ token } ` , getBaseUrl ( ) ) )
3934 }
4035 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
4136 }
@@ -54,10 +49,7 @@ export async function GET(
5449 if ( ! invitation ) {
5550 if ( isAcceptFlow ) {
5651 return NextResponse . redirect (
57- new URL (
58- `/invite/${ invitationId } ?error=invalid-token` ,
59- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
60- )
52+ new URL ( `/invite/${ invitationId } ?error=invalid-token` , getBaseUrl ( ) )
6153 )
6254 }
6355 return NextResponse . json ( { error : 'Invitation not found or has expired' } , { status : 404 } )
@@ -66,10 +58,7 @@ export async function GET(
6658 if ( new Date ( ) > new Date ( invitation . expiresAt ) ) {
6759 if ( isAcceptFlow ) {
6860 return NextResponse . redirect (
69- new URL (
70- `/invite/${ invitation . id } ?error=expired` ,
71- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
72- )
61+ new URL ( `/invite/${ invitation . id } ?error=expired` , getBaseUrl ( ) )
7362 )
7463 }
7564 return NextResponse . json ( { error : 'Invitation has expired' } , { status : 400 } )
@@ -84,10 +73,7 @@ export async function GET(
8473 if ( ! workspaceDetails ) {
8574 if ( isAcceptFlow ) {
8675 return NextResponse . redirect (
87- new URL (
88- `/invite/${ invitation . id } ?error=workspace-not-found` ,
89- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
90- )
76+ new URL ( `/invite/${ invitation . id } ?error=workspace-not-found` , getBaseUrl ( ) )
9177 )
9278 }
9379 return NextResponse . json ( { error : 'Workspace not found' } , { status : 404 } )
@@ -96,10 +82,7 @@ export async function GET(
9682 if ( isAcceptFlow ) {
9783 if ( invitation . status !== ( 'pending' as WorkspaceInvitationStatus ) ) {
9884 return NextResponse . redirect (
99- new URL (
100- `/invite/${ invitation . id } ?error=already-processed` ,
101- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
102- )
85+ new URL ( `/invite/${ invitation . id } ?error=already-processed` , getBaseUrl ( ) )
10386 )
10487 }
10588
@@ -114,21 +97,15 @@ export async function GET(
11497
11598 if ( ! userData ) {
11699 return NextResponse . redirect (
117- new URL (
118- `/invite/${ invitation . id } ?error=user-not-found` ,
119- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
120- )
100+ new URL ( `/invite/${ invitation . id } ?error=user-not-found` , getBaseUrl ( ) )
121101 )
122102 }
123103
124104 const isValidMatch = userEmail === invitationEmail
125105
126106 if ( ! isValidMatch ) {
127107 return NextResponse . redirect (
128- new URL (
129- `/invite/${ invitation . id } ?error=email-mismatch` ,
130- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
131- )
108+ new URL ( `/invite/${ invitation . id } ?error=email-mismatch` , getBaseUrl ( ) )
132109 )
133110 }
134111
@@ -154,10 +131,7 @@ export async function GET(
154131 . where ( eq ( workspaceInvitation . id , invitation . id ) )
155132
156133 return NextResponse . redirect (
157- new URL (
158- `/workspace/${ invitation . workspaceId } /w` ,
159- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
160- )
134+ new URL ( `/workspace/${ invitation . workspaceId } /w` , getBaseUrl ( ) )
161135 )
162136 }
163137
@@ -181,12 +155,7 @@ export async function GET(
181155 . where ( eq ( workspaceInvitation . id , invitation . id ) )
182156 } )
183157
184- return NextResponse . redirect (
185- new URL (
186- `/workspace/${ invitation . workspaceId } /w` ,
187- env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
188- )
189- )
158+ return NextResponse . redirect ( new URL ( `/workspace/${ invitation . workspaceId } /w` , getBaseUrl ( ) ) )
190159 }
191160
192161 return NextResponse . json ( {
@@ -298,7 +267,7 @@ export async function POST(
298267 . set ( { token : newToken , expiresAt : newExpiresAt , updatedAt : new Date ( ) } )
299268 . where ( eq ( workspaceInvitation . id , invitationId ) )
300269
301- const baseUrl = env . NEXT_PUBLIC_APP_URL || 'https://sim.ai'
270+ const baseUrl = getBaseUrl ( )
302271 const invitationLink = `${ baseUrl } /invite/${ invitationId } ?token=${ newToken } `
303272
304273 const emailHtml = await render (
0 commit comments