A social knowledge tool for researchers built on ATProto
at ff03e09bfaf3b3baf2f90cdc6562677f0331ff67 56 lines 1.5 kB view raw
1import { ServerCookieAuthService } from '@/services/auth/CookieAuthService.server'; 2import type { GetProfileResponse } from '@/api-client/ApiClient'; 3 4type UserProfile = GetProfileResponse; 5 6export async function getServerAuthStatus(): Promise<{ 7 isAuthenticated: boolean; 8 user: UserProfile | null; 9 error: string | null; 10}> { 11 try { 12 const { accessToken } = await ServerCookieAuthService.getTokens(); 13 14 if (!accessToken || ServerCookieAuthService.isTokenExpired(accessToken)) { 15 return { 16 isAuthenticated: false, 17 user: null, 18 error: 'No valid access token', 19 }; 20 } 21 22 // Make direct API call with cookie header for server-side 23 const baseUrl = 24 process.env.NEXT_PUBLIC_API_BASE_URL || 'http://127.0.0.1:3000'; 25 const response = await fetch(`${baseUrl}/api/users/me`, { 26 method: 'GET', 27 headers: { 28 'Content-Type': 'application/json', 29 Cookie: `accessToken=${accessToken}`, 30 }, 31 cache: 'no-store', 32 }); 33 34 if (!response.ok) { 35 return { 36 isAuthenticated: false, 37 user: null, 38 error: `API request failed: ${response.status}`, 39 }; 40 } 41 42 const user: UserProfile = await response.json(); 43 44 return { 45 isAuthenticated: true, 46 user, 47 error: null, 48 }; 49 } catch (error: any) { 50 return { 51 isAuthenticated: false, 52 user: null, 53 error: error.message || 'Authentication failed', 54 }; 55 } 56}