/** * Format token response for the client * * Supports two response formats: * 1. JSON response (for API clients) * 2. Redirect with token in URL fragment (for web browsers) * * URL fragments are used for security - they are NOT sent to the server * in HTTP requests and are only accessible to client-side JavaScript. * * @param token - JWT token for the application * @param user - User object * @param redirectUrl - URL to redirect to * @param responseType - "json" or undefined (redirect) * @returns Flink response object */ export function formatTokenResponse(token: string, user: any, redirectUrl: string, responseType?: "json"): any { // JSON response for API clients if (responseType === "json") { return { data: { user, token, }, }; } // Redirect response for web browsers // Token is in URL fragment (#token=...) for security const separator = redirectUrl.includes("#") ? "&" : "#"; const tokenFragment = `token=${encodeURIComponent(token)}`; const finalUrl = `${redirectUrl}${separator}${tokenFragment}`; return { status: 302, headers: { Location: finalUrl, }, data: {}, }; }