mirror of
https://github.com/freedbygrace/ActiveDirectoryManager.git
synced 2026-08-21 15:47:41 +00:00
Fix authentication issues causing registration and login failures by updating authentication middleware to handle errors more robustly and avoid relying on req.isAuthenticated.
Replit-Commit-Author: Agent Replit-Commit-Session-Id: 705f2157-ef97-4fbd-89e4-8c7f2ecaea90 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7ed01c5f-a82d-405a-b728-b2e3d127c60c/dfc84194-36f5-44cc-b11c-3358e4dbd499.jpg
This commit is contained in:
+54
-25
@@ -137,20 +137,40 @@ export function setupAuth(app: Express) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Login endpoint
|
// Login endpoint - fixed without relying on req.isAuthenticated
|
||||||
app.post("/api/login", (req, res, next) => {
|
app.post("/api/login", (req, res, next) => {
|
||||||
passport.authenticate("local", (err: any, user: any, info: any) => {
|
try {
|
||||||
if (err) return next(err);
|
passport.authenticate("local", (err: any, user: any, info: any) => {
|
||||||
if (!user) {
|
if (err) {
|
||||||
return res.status(401).json({ message: info?.message || "Authentication failed" });
|
console.error("Authentication error:", err);
|
||||||
}
|
return res.status(500).json({ message: "Internal server error during authentication" });
|
||||||
req.login(user, (loginErr) => {
|
}
|
||||||
if (loginErr) return next(loginErr);
|
|
||||||
// Remove password from response
|
if (!user) {
|
||||||
const userResponse = { ...user, password: undefined };
|
return res.status(401).json({ message: info?.message || "Authentication failed" });
|
||||||
res.json(userResponse);
|
}
|
||||||
});
|
|
||||||
})(req, res, next);
|
// Manual login with try-catch to safely handle errors
|
||||||
|
try {
|
||||||
|
req.login(user, (loginErr) => {
|
||||||
|
if (loginErr) {
|
||||||
|
console.error("Login error:", loginErr);
|
||||||
|
return res.status(500).json({ message: "Error during login process" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove password from response
|
||||||
|
const userResponse = { ...user, password: undefined };
|
||||||
|
return res.json(userResponse);
|
||||||
|
});
|
||||||
|
} catch (loginError) {
|
||||||
|
console.error("Exception during login:", loginError);
|
||||||
|
return res.status(500).json({ message: "Login process failed" });
|
||||||
|
}
|
||||||
|
})(req, res, next);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Unexpected error in login route:", error);
|
||||||
|
return res.status(500).json({ message: "Internal server error" });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Logout endpoint
|
// Logout endpoint
|
||||||
@@ -165,23 +185,31 @@ export function setupAuth(app: Express) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get current user endpoint
|
// Get current user endpoint - fixed without relying on req.isAuthenticated
|
||||||
app.get("/api/user", (req, res) => {
|
app.get("/api/user", (req, res) => {
|
||||||
if (!req.isAuthenticated()) {
|
try {
|
||||||
return res.status(401).json({ message: "Not authenticated" });
|
// Check if user exists in session instead of using isAuthenticated
|
||||||
|
if (!req.user) {
|
||||||
|
return res.status(401).json({ message: "Not authenticated" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove password from response
|
||||||
|
const userResponse = { ...req.user, password: undefined };
|
||||||
|
res.json(userResponse);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error in user endpoint:", error);
|
||||||
|
res.status(500).json({ message: "Internal server error" });
|
||||||
}
|
}
|
||||||
// Remove password from response
|
|
||||||
const userResponse = { ...req.user, password: undefined };
|
|
||||||
res.json(userResponse);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Generate API token endpoint
|
// Generate API token endpoint - fixed without relying on req.isAuthenticated
|
||||||
app.post("/api/tokens", (req, res, next) => {
|
app.post("/api/tokens", (req, res, next) => {
|
||||||
if (!req.isAuthenticated()) {
|
|
||||||
return res.status(401).json({ message: "Not authenticated" });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Check if user exists in session
|
||||||
|
if (!req.user) {
|
||||||
|
return res.status(401).json({ message: "Not authenticated" });
|
||||||
|
}
|
||||||
|
|
||||||
const { name, expiresAt, roleId, customPermissions } = req.body;
|
const { name, expiresAt, roleId, customPermissions } = req.body;
|
||||||
if (!name) {
|
if (!name) {
|
||||||
return res.status(400).json({ message: "Token name is required" });
|
return res.status(400).json({ message: "Token name is required" });
|
||||||
@@ -211,7 +239,8 @@ export function setupAuth(app: Express) {
|
|||||||
|
|
||||||
res.status(201).json(apiToken);
|
res.status(201).json(apiToken);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
console.error("Error generating API token:", error);
|
||||||
|
res.status(500).json({ message: "Failed to generate API token" });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+53
-48
@@ -16,61 +16,66 @@ export type RequirePermissionOptions = RequireAuthOptions & {
|
|||||||
// Default middleware that verifies a user is authenticated
|
// Default middleware that verifies a user is authenticated
|
||||||
export function requireAuth(options: RequireAuthOptions = {}) {
|
export function requireAuth(options: RequireAuthOptions = {}) {
|
||||||
return async (req: Request, res: Response, next: NextFunction) => {
|
return async (req: Request, res: Response, next: NextFunction) => {
|
||||||
// Check for session authentication
|
try {
|
||||||
if (typeof req.isAuthenticated === 'function' && req.isAuthenticated()) {
|
// Check for session authentication - just check if req.user exists
|
||||||
return next();
|
if (req.user) {
|
||||||
}
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
// Check for API token authentication if allowed
|
// Check for API token authentication if allowed
|
||||||
if (options.allowApiToken) {
|
if (options.allowApiToken) {
|
||||||
const authHeader = req.headers.authorization;
|
const authHeader = req.headers.authorization;
|
||||||
if (authHeader && authHeader.startsWith("Bearer ")) {
|
if (authHeader && authHeader.startsWith("Bearer ")) {
|
||||||
const token = authHeader.substring(7);
|
const token = authHeader.substring(7);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Lookup the token
|
// Lookup the token
|
||||||
const [apiToken] = await db.select()
|
const [apiToken] = await db.select()
|
||||||
.from(apiTokens)
|
.from(apiTokens)
|
||||||
.where(eq(apiTokens.token, token))
|
.where(eq(apiTokens.token, token))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if (!apiToken) {
|
if (!apiToken) {
|
||||||
return res.status(401).json({ message: "Invalid API token" });
|
return res.status(401).json({ message: "Invalid API token" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if token has expired
|
||||||
|
if (apiToken.expiresAt && new Date(apiToken.expiresAt) < new Date()) {
|
||||||
|
return res.status(401).json({ message: "API token has expired" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set token info on the request
|
||||||
|
req.user = {
|
||||||
|
id: apiToken.userId,
|
||||||
|
username: '',
|
||||||
|
password: '',
|
||||||
|
fullName: null,
|
||||||
|
email: null,
|
||||||
|
createdAt: null,
|
||||||
|
// Add tokenId for identifying which token was used
|
||||||
|
tokenId: apiToken.id,
|
||||||
|
// Add roleId for permission checking
|
||||||
|
roleId: apiToken.roleId,
|
||||||
|
// Store custom permissions if available
|
||||||
|
customPermissions: (Array.isArray(apiToken.customPermissions) ?
|
||||||
|
apiToken.customPermissions :
|
||||||
|
(apiToken.customPermissions ? JSON.parse(String(apiToken.customPermissions)) : [])) as string[]
|
||||||
|
};
|
||||||
|
|
||||||
|
return next();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("API token authentication error:", error);
|
||||||
|
return res.status(500).json({ message: "Internal server error" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if token has expired
|
|
||||||
if (apiToken.expiresAt && new Date(apiToken.expiresAt) < new Date()) {
|
|
||||||
return res.status(401).json({ message: "API token has expired" });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set token info on the request
|
|
||||||
req.user = {
|
|
||||||
id: apiToken.userId,
|
|
||||||
username: '',
|
|
||||||
password: '',
|
|
||||||
fullName: null,
|
|
||||||
email: null,
|
|
||||||
createdAt: null,
|
|
||||||
// Add tokenId for identifying which token was used
|
|
||||||
tokenId: apiToken.id,
|
|
||||||
// Add roleId for permission checking
|
|
||||||
roleId: apiToken.roleId,
|
|
||||||
// Store custom permissions if available
|
|
||||||
customPermissions: (Array.isArray(apiToken.customPermissions) ?
|
|
||||||
apiToken.customPermissions :
|
|
||||||
(apiToken.customPermissions ? JSON.parse(String(apiToken.customPermissions)) : [])) as string[]
|
|
||||||
};
|
|
||||||
|
|
||||||
return next();
|
|
||||||
} catch (error) {
|
|
||||||
console.error("API token authentication error:", error);
|
|
||||||
return res.status(500).json({ message: "Internal server error" });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Not authenticated through any method
|
// Not authenticated through any method
|
||||||
return res.status(401).json({ message: "Not authenticated" });
|
return res.status(401).json({ message: "Not authenticated" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Authentication error:", error);
|
||||||
|
return res.status(500).json({ message: "Internal server error during authentication" });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user