File size: 1,494 Bytes
bdc8b77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
import { Request, Response, NextFunction } from "express";
import { toFetchHeaders } from "./to-fetch-headers";
import { auth } from "../auth";
import { redis } from "../../infrastructure/redis/redis-client";
import { extractToken } from "./auth-utils";
import { Keys } from "../keys";

export const currentUser = async (
  req: Request,
  res: Response,
  next: NextFunction,
) => {
  try {
    const token = extractToken(req);

    if (!token) {
      return res
        .status(401)
        .json({ error: "Unauthenticated: Session token missing" });
    }

    const cacheKey = Keys.session(token);
    const cachedSession = await redis.get(cacheKey);

    if (cachedSession) {
      const parsed = JSON.parse(cachedSession);
      req.user = parsed.user;
      return next();
    }

    const session = await auth.api.getSession({
      headers: toFetchHeaders(req.headers),
    });

    if (!session?.user) {
      return res
        .status(401)
        .json({ error: "Unauthenticated: Invalid session" });
    }

    req.user = session.user;
    const expiresAt = new Date(session.session.expiresAt).getTime();
    const now = Date.now();
    const remainingTTL = Math.floor((expiresAt - now) / 1000);

    if (remainingTTL > 0) {
      await redis.set(cacheKey, JSON.stringify(session), "EX", remainingTTL);
    }
    return next();
  } catch (err) {
    console.error("Error resolving user context:", err);
    return res.status(500).json({ error: "Internal Server Error" });
  }
};