feat: auth foundation, similar tools, costs, redundancy, anonymous voting
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { type Request, type Response, type NextFunction } from "express";
|
||||
|
||||
const TIER_FEATURES: Record<string, string[]> = {
|
||||
free: ["browse", "rate", "search"],
|
||||
premium: ["browse", "rate", "search", "similar-tools", "costs", "redundancy", "analytics-advanced"],
|
||||
enterprise: ["browse", "rate", "search", "similar-tools", "costs", "redundancy", "analytics-advanced", "sso", "audit-export", "api-access"],
|
||||
};
|
||||
|
||||
export function hasFeature(tier: string | undefined, feature: string): boolean {
|
||||
const features = TIER_FEATURES[tier ?? "free"] ?? TIER_FEATURES.free;
|
||||
return features.includes(feature);
|
||||
}
|
||||
|
||||
export function requireFeature(feature: string) {
|
||||
return (req: Request, res: Response, next: NextFunction): void => {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ error: "Authentication required" });
|
||||
return;
|
||||
}
|
||||
if (!hasFeature(req.session.user.tier, feature)) {
|
||||
res.status(403).json({ error: `Feature "${feature}" requires a higher tier` });
|
||||
return;
|
||||
}
|
||||
next();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user