ATlast — you'll never need to find your favorites on another platform again. Find your favs in the ATmosphere.
atproto
1import { SimpleHandler } from "./core/types/api.types";
2import { createOAuthClient } from "./infrastructure/oauth/OAuthClientFactory";
3import { successResponse } from "./utils";
4import { withErrorHandling } from "./core/middleware";
5import { ValidationError, ApiError } from "./core/errors";
6
7interface OAuthStartRequestBody {
8 login_hint?: string;
9 origin?: string;
10}
11
12const oauthStartHandler: SimpleHandler = async (event) => {
13 let loginHint: string | undefined = undefined;
14
15 try {
16 if (event.body) {
17 const parsed: OAuthStartRequestBody = JSON.parse(event.body);
18 loginHint = parsed.login_hint;
19 }
20
21 if (!loginHint) {
22 throw new ValidationError("login_hint (handle or DID) is required");
23 }
24
25 console.log("[oauth-start] Starting OAuth flow for:", loginHint);
26
27 let client;
28 try {
29 client = await createOAuthClient(event);
30 console.log("[oauth-start] OAuth client created successfully");
31 } catch (clientError) {
32 console.error(
33 "[oauth-start] Failed to create OAuth client:",
34 clientError instanceof Error
35 ? clientError.message
36 : String(clientError),
37 );
38 throw new ApiError(
39 "Failed to create OAuth client",
40 500,
41 clientError instanceof Error ? clientError.message : "Unknown error",
42 );
43 }
44
45 let authUrl;
46 try {
47 authUrl = await client.authorize(loginHint, {
48 scope: "atproto transition:generic",
49 });
50 console.log("[oauth-start] Generated auth URL successfully");
51 } catch (authorizeError) {
52 console.error(
53 "[oauth-start] Failed to authorize:",
54 authorizeError instanceof Error
55 ? authorizeError.message
56 : String(authorizeError),
57 );
58 throw new ApiError(
59 "Failed to generate authorization URL",
60 500,
61 authorizeError instanceof Error
62 ? authorizeError.message
63 : "Unknown error",
64 );
65 }
66
67 console.log("[oauth-start] Returning auth URL for:", loginHint);
68 return successResponse({ url: authUrl.toString() });
69 } catch (error) {
70 // This will be caught by withErrorHandling, but log it here too for clarity
71 console.error(
72 "[oauth-start] Top-level error:",
73 error instanceof Error ? error.message : String(error),
74 );
75 throw error;
76 }
77};
78
79export const handler = withErrorHandling(oauthStartHandler);