ATlast — you'll never need to find your favorites on another platform again. Find your favs in the ATmosphere.
atproto
at master 2.6 kB view raw
1import { SimpleHandler } from "./core/types/api.types"; 2import { createOAuthClient, getOAuthConfig } from "./infrastructure/oauth"; 3import { createSecureSessionData } from "./core/middleware/session-security.middleware"; 4import { userSessions } from "./infrastructure/oauth/stores"; 5import { redirectResponse } from "./utils"; 6import { withErrorHandling } from "./core/middleware"; 7import { CONFIG } from "./core/config/constants"; 8import * as crypto from "crypto"; 9 10const oauthCallbackHandler: SimpleHandler = async (event) => { 11 const config = getOAuthConfig(event); 12 const isDev = config.clientType === "loopback"; 13 14 // Land back on the same host you started from 15 let currentUrl = config.redirectUri.replace( 16 "/.netlify/functions/oauth-callback", 17 "", 18 ); 19 20 const params = new URLSearchParams(event.rawUrl.split("?")[1] || ""); 21 const code = params.get("code"); 22 const state = params.get("state"); 23 24 console.log( 25 "[oauth-callback] Processing callback - Mode:", 26 isDev ? "loopback" : "production", 27 ); 28 console.log("[oauth-callback] URL:", currentUrl); 29 30 if (!code || !state) { 31 return redirectResponse(`${currentUrl}/?error=Missing OAuth parameters`); 32 } 33 34 const client = await createOAuthClient(event); 35 36 const result = await client.callback(params); 37 38 console.log( 39 "[oauth-callback] Successfully authenticated DID:", 40 result.session.did, 41 ); 42 43 const sessionId = crypto.randomUUID(); 44 const secureData = createSecureSessionData(event, result.session.did); 45 46 await userSessions.set(sessionId, { 47 did: secureData.did, 48 fingerprint: secureData.fingerprint, 49 }); 50 51 console.log("[oauth-callback] Created user session:", sessionId); 52 53 // Determine cookie configuration 54 // Use DEPLOY_URL to detect Netlify Live mode 55 const isNetlifyLive = (process.env.DEPLOY_URL || process.env.URL)?.includes( 56 ".netlify.live", 57 ); 58 const isSecure = currentUrl.startsWith("https://") || isNetlifyLive; 59 60 // Use dev cookie for development, otherwise production cookie 61 const cookieName = 62 isDev && !isNetlifyLive ? "atlast_session_dev" : "atlast_session"; 63 const cookieFlags = isSecure 64 ? `HttpOnly; SameSite=Lax; Max-Age=${CONFIG.COOKIE_MAX_AGE}; Path=/; Secure` 65 : `HttpOnly; SameSite=Lax; Max-Age=${CONFIG.COOKIE_MAX_AGE}; Path=/`; 66 67 console.log( 68 "[oauth-callback] Setting cookie:", 69 cookieName, 70 "for URL:", 71 currentUrl, 72 ); 73 74 return redirectResponse( 75 `${currentUrl}/?session=${sessionId}`, 76 `${cookieName}=${sessionId}; ${cookieFlags}`, 77 ); 78}; 79 80export const handler = withErrorHandling(oauthCallbackHandler);