forked from
nekomimi.pet/wisp.place-monorepo
Monorepo for Wisp.place. A static site hosting service built on top of the AT Protocol.
1import { Did } from "@atproto/api";
2import { NodeOAuthClient } from "@atproto/oauth-client-node";
3import type { OAuthSession } from "@atproto/oauth-client-node";
4import { Cookie } from "elysia";
5import { logger } from "./logger";
6
7
8export interface AuthenticatedContext {
9 did: Did;
10 session: OAuthSession;
11}
12
13export const authenticateRequest = async (
14 client: NodeOAuthClient,
15 cookies: Record<string, Cookie<unknown>>
16): Promise<AuthenticatedContext | null> => {
17 try {
18 const did = cookies.did?.value as Did;
19 if (!did) return null;
20
21 const session = await client.restore(did, "auto");
22 return session ? { did, session } : null;
23 } catch (err) {
24 logger.error('[Auth] Authentication error', err);
25 return null;
26 }
27};
28
29export const requireAuth = async (
30 client: NodeOAuthClient,
31 cookies: Record<string, Cookie<unknown>>
32): Promise<AuthenticatedContext> => {
33 const auth = await authenticateRequest(client, cookies);
34 if (!auth) {
35 throw new Error('Authentication required');
36 }
37 return auth;
38};