MCPcopy Create free account
hub / github.com/code100x/mobile-magic / authMiddleware

Function authMiddleware

mobile-magic/packages/common/middleware.ts:14–41  ·  view source on GitHub ↗
(req: Request, res: Response, next: NextFunction)

Source from the content-addressed store, hash-verified

12-----END PUBLIC KEY-----`;
13
14export function authMiddleware(req: Request, res: Response, next: NextFunction) {
15 const authHeader = req.headers.authorization; // Bearer token
16 const token = authHeader && authHeader.split(" ")[1];
17
18 if (!token) {
19 res.status(401).json({ message: "Unauthorized" });
20 return;
21 }
22
23 const decoded = jwt.verify(token, JWT_PUBLIC_KEY!, {
24 algorithms: ["RS256"],
25 });
26
27 if (!decoded) {
28 res.status(401).json({ message: "Unauthorized" });
29 return;
30 }
31
32 const userId = (decoded as any).sub;
33
34 if (!userId) {
35 res.status(401).json({ message: "Unauthorized" });
36 return;
37 }
38
39 req.userId = userId;
40 next();
41}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected