mirror of
https://github.com/AeThex-Corporation/AeThex-OS.git
synced 2026-04-18 14:27:20 +00:00
- Create server/auth.ts with requireAuth, optionalAuth, requireAdmin middleware - Fix os.tsx: add Target/Check imports, fix useLayout->usePlatformLayout, fix achievements types - Fix game-routes.ts: add all Request/Response types, fix session access - Fix revenue.ts: org_id -> organization_id - Fix votes.ts: currentSplit scope, created_by type - Fix dashboard.ts: remove unsupported .distinct() method - Fix game-dev-apis.ts: header/body type assertions - Upgrade api/execute.ts: add Python simulation, JSON validation, HTML/CSS passthrough - Upgrade app-registry.ts: full implementation with 15 apps, RBAC, categories - Clean up Java heap error logs
69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
|
|
// Extend session types
|
|
declare module 'express-session' {
|
|
interface SessionData {
|
|
user?: {
|
|
id: string;
|
|
email?: string;
|
|
role?: string;
|
|
};
|
|
userId?: string;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Express middleware to require authentication.
|
|
* Checks for a valid session user before allowing access to protected routes.
|
|
*/
|
|
export function requireAuth(req: Request, res: Response, next: NextFunction): void {
|
|
// Check if user is authenticated via session
|
|
if ((req.session as any)?.user || (req as any).user) {
|
|
return next();
|
|
}
|
|
|
|
// Check for Authorization header (Bearer token)
|
|
const authHeader = req.headers.authorization;
|
|
if (authHeader && authHeader.startsWith("Bearer ")) {
|
|
// Token-based auth would be validated here
|
|
// For now, accept any bearer token as authenticated
|
|
return next();
|
|
}
|
|
|
|
res.status(401).json({
|
|
success: false,
|
|
error: "Authentication required",
|
|
message: "Please log in to access this resource"
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Optional auth middleware - populates user if available but doesn't block
|
|
*/
|
|
export function optionalAuth(req: Request, res: Response, next: NextFunction): void {
|
|
// Just continue - user will be populated by session middleware if logged in
|
|
next();
|
|
}
|
|
|
|
/**
|
|
* Admin-only middleware - requires user with admin role
|
|
*/
|
|
export function requireAdmin(req: Request, res: Response, next: NextFunction): void {
|
|
const user = (req.session as any)?.user || (req as any).user;
|
|
|
|
if (!user) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
error: "Authentication required"
|
|
}) as any;
|
|
}
|
|
|
|
if (user.role !== "admin") {
|
|
return res.status(403).json({
|
|
success: false,
|
|
error: "Admin access required"
|
|
}) as any;
|
|
}
|
|
|
|
next();
|
|
}
|