1- "use server" ;
2-
31import { headers } from "next/headers" ;
4- import { OrigamiUser , OrigamiSession , auth } from "@repo/database" ;
2+ import type { OrigamiUser , OrigamiSession } from "@repo/database" ;
53
64import { getAPIBaseUrl } from "@/lib/url" ;
75import { redirect } from "next/navigation" ;
@@ -12,54 +10,21 @@ export async function getAuth(): Promise<{
1210} > {
1311 try {
1412 const headersList = await headers ( ) ;
15-
16- // First try the built-in Better Auth API
17- try {
18- const session = await auth . api . getSession ( {
19- headers : headersList ,
20- } ) ;
21-
22- if ( session ) {
23- console . log ( "session from auth.api.getSession" , session ) ;
24- return session ?? { user : null , session : null } ;
25- }
26- } catch ( authApiError ) {
27- console . warn (
28- "auth.api.getSession failed, falling back to manual fetch:" ,
29- authApiError
30- ) ;
31- }
32-
33- // Fallback to manual fetch for cross-origin API calls
3413 const cookie = headersList . get ( "cookie" ) ;
3514
3615 const session = await fetch ( getAPIBaseUrl ( "/auth/get-session" ) , {
37- method : "GET" ,
3816 headers : {
3917 "Content-Type" : "application/json" ,
40- Cookie : cookie ?? "" ,
41- // Add origin header for cross-origin requests
42- Origin :
43- process . env . NODE_ENV === "production"
44- ? "https://www.origamichat.com"
45- : "http://localhost:3000" ,
18+ cookie : cookie ?? "" ,
4619 } ,
4720 credentials : "include" ,
48- // Add cache control for SSR
4921 cache : "no-store" ,
50- } ) . then ( async ( res ) => {
51- if ( ! res . ok ) {
52- console . error ( "Session fetch failed:" , res . status , res . statusText ) ;
53- return null ;
54- }
55- return res . json ( ) ;
56- } ) ;
57-
58- console . log ( "session from manual fetch" , session ) ;
22+ } ) . then ( ( res ) => res . json ( ) ) ;
5923
24+ console . log ( "session" , session ) ;
6025 return session ?? { user : null , session : null } ;
6126 } catch ( error ) {
62- console . error ( "Both auth methods failed:" , error ) ;
27+ console . error ( error ) ;
6328 return { user : null , session : null } ;
6429 }
6530}
0 commit comments