File size: 1,317 Bytes
ccc21f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { Request, Response, NextFunction } from "express";
import { getAuth, clerkClient } from "@clerk/express";

export interface AuthedRequest extends Request {
  userId: string;
  userEmail: string | null;
}

const emailCache = new Map<string, { email: string | null; ts: number }>();
const CACHE_TTL_MS = 15 * 60 * 1000;

export async function requireAuth(
  req: Request,
  res: Response,
  next: NextFunction,
): Promise<void> {
  const auth = getAuth(req);
  const claimUserId =
    typeof auth?.sessionClaims?.["userId"] === "string"
      ? (auth.sessionClaims["userId"] as string)
      : null;
  const userId: string | null = claimUserId ?? auth?.userId ?? null;
  if (!userId) {
    res.status(401).json({ error: "Unauthorized" });
    return;
  }

  let email: string | null = null;
  const cached = emailCache.get(userId);
  if (cached && Date.now() - cached.ts < CACHE_TTL_MS) {
    email = cached.email;
  } else {
    try {
      const user = await clerkClient.users.getUser(userId);
      email = user.primaryEmailAddress?.emailAddress ?? null;
    } catch (err) {
      req.log.warn({ err }, "Failed to fetch user from Clerk");
    }
    emailCache.set(userId, { email, ts: Date.now() });
  }

  (req as AuthedRequest).userId = userId;
  (req as AuthedRequest).userEmail = email;
  next();
}