|
1
|
import type { RequestHandler } from "express"; |
|
2
|
import { storage } from "../storage"; |
|
3
|
|
|
4
|
export const requireAdmin: RequestHandler = async (req, res, next) => { |
|
5
|
const user = req.user as any; |
|
6
|
|
|
7
|
// Firebase auth attaches user object with id to request |
|
8
|
if (!user || !user.id) { |
|
9
|
return res.status(401).json({ message: "Unauthorized: Admin access requires authentication" }); |
|
10
|
} |
|
11
|
|
|
12
|
try { |
|
13
|
const dbUser = await storage.getUser(user.id); |
|
14
|
|
|
15
|
if (!dbUser || dbUser.role !== "admin") { |
|
16
|
return res.status(403).json({ message: "Admin access required" }); |
|
17
|
} |
|
18
|
|
|
19
|
// Attach user info to request for convenience |
|
20
|
req.adminUser = dbUser; |
|
21
|
next(); |
|
22
|
} catch (error) { |
|
23
|
console.error("Error checking admin status:", error); |
|
24
|
res.status(500).json({ message: "Failed to verify admin status" }); |
|
25
|
} |
|
26
|
}; |
|
27
|
|
|
28
|
// Type declaration for TypeScript |
|
29
|
declare global { |
|
30
|
namespace Express { |
|
31
|
interface Request { |
|
32
|
adminUser?: { |
|
33
|
id: string; |
|
34
|
email: string | null; |
|
35
|
firstName: string | null; |
|
36
|
lastName: string | null; |
|
37
|
profileImageUrl: string | null; |
|
38
|
role: string | null; |
|
39
|
createdAt: Date | null; |
|
40
|
updatedAt: Date | null; |
|
41
|
}; |
|
42
|
} |
|
43
|
} |
|
44
|
} |