1
+ import { headers } from "next/headers" ;
1
2
import { redirect } from "next/navigation" ;
2
3
3
4
import { Auth0Client } from "@auth0/nextjs-auth0/server" ;
@@ -15,25 +16,30 @@ if (ZACH_TEST_APP_BASE_URL == null) {
15
16
throw new Error ( "ZACH_TEST_APP_BASE_URL is not defined in the environment" ) ;
16
17
}
17
18
18
- console . log (
19
- { NEXT_PUBLIC_VENUS_AUDIENCE , ZACH_TEST_APP_BASE_URL } ,
20
- process . env
21
- ) ;
22
-
23
- export const auth0 = new Auth0Client ( {
24
- async beforeSessionSaved ( session , idToken ) {
25
- return {
26
- ...session ,
27
- idToken,
28
- } ;
29
- } ,
30
- authorizationParameters : {
31
- audience : NEXT_PUBLIC_VENUS_AUDIENCE ,
32
- } ,
33
- appBaseUrl : ZACH_TEST_APP_BASE_URL ,
34
- } ) ;
19
+ export async function getAuth0Client ( ) {
20
+ return new Auth0Client ( {
21
+ authorizationParameters : {
22
+ audience : NEXT_PUBLIC_VENUS_AUDIENCE ,
23
+ } ,
24
+ appBaseUrl : await getBaseUrl ( ) ,
25
+ } ) ;
26
+ }
27
+
28
+ async function getBaseUrl ( ) : Promise < string > {
29
+ const headersList = await headers ( ) ;
30
+
31
+ const host = headersList . get ( "host" ) ;
32
+ if ( host == null ) {
33
+ throw new Error ( "host header is not present" ) ;
34
+ }
35
+
36
+ const protocol = headersList . get ( "x-forwarded-proto" ) ?? "https" ;
37
+
38
+ return `${ protocol } ://${ host } ` ;
39
+ }
35
40
36
41
export async function getSessionOrRedirect ( ) {
42
+ const auth0 = await getAuth0Client ( ) ;
37
43
const session = await auth0 . getSession ( ) ;
38
44
if ( session == null ) {
39
45
redirect ( "/" ) ;
0 commit comments