1
1
import 'server-only' ;
2
2
import { cookies , headers as nextHeaders } from 'next/headers' ;
3
3
4
- function getBaseUrl ( ) {
4
+ function getPlatformUrl ( ) {
5
5
if ( process . env . NEXT_PUBLIC_PLATFORM_URL )
6
6
return process . env . NEXT_PUBLIC_PLATFORM_URL ;
7
7
throw new Error ( 'NEXT_PUBLIC_PLATFORM_URL is not set' ) ;
8
8
}
9
9
10
+ function clientHeaders ( ) {
11
+ const allHeaders = nextHeaders ( ) ;
12
+ const clientHeaders = new Headers ( ) ;
13
+ const notAllowedHeaders = [ 'host' , 'origin' , 'referer' ] ;
14
+ allHeaders . forEach ( ( value , key ) => {
15
+ if ( notAllowedHeaders . includes ( key ) ) return ;
16
+ clientHeaders . append ( key , value ) ;
17
+ } ) ;
18
+ return clientHeaders ;
19
+ }
20
+
10
21
export async function getAuthRedirection ( ) {
11
22
if ( ! cookies ( ) . has ( 'unsession' ) ) return { defaultOrgShortcode : null } ;
12
- return fetch ( `${ getBaseUrl ( ) } /auth/redirection` , {
13
- headers : nextHeaders ( )
23
+ return fetch ( `${ getPlatformUrl ( ) } /auth/redirection` , {
24
+ headers : clientHeaders ( )
14
25
} ) . then ( ( r ) => ( r . ok ? r . json ( ) : { defaultOrgShortcode : null } ) ) as Promise < {
15
26
defaultOrgShortcode : string | null ;
16
27
} > ;
@@ -22,8 +33,8 @@ export async function isAuthenticated(shallow = false) {
22
33
if ( ! cookies ( ) . has ( 'unsession' ) ) return false ;
23
34
if ( shallow ) return true ;
24
35
try {
25
- const data = ( await fetch ( `${ getBaseUrl ( ) } /auth/status` , {
26
- headers : nextHeaders ( )
36
+ const data = ( await fetch ( `${ getPlatformUrl ( ) } /auth/status` , {
37
+ headers : clientHeaders ( )
27
38
} ) . then ( ( r ) => r . json ( ) ) ) as {
28
39
authStatus : 'authenticated' | 'unauthenticated' ;
29
40
} ;
0 commit comments