feat(auth): enhance login and verification processes with detailed debug logging
- Added comprehensive debug logging to the login strategy and verification endpoint to track authentication flow and errors. - Improved title determination logic for login and signup success/failure messages based on authentication status. - Implemented middleware for logging request details on the login route to aid in debugging.
This commit is contained in:
@@ -35,8 +35,32 @@ export default class AuthController extends WorklenzControllerBase {
|
||||
const auth_error = errors.length > 0 ? errors[0] : null;
|
||||
const message = messages.length > 0 ? messages[0] : null;
|
||||
|
||||
const midTitle = req.query.strategy === "login" ? "Login Failed!" : "Signup Failed!";
|
||||
const title = req.query.strategy ? midTitle : null;
|
||||
// Debug logging
|
||||
console.log("=== VERIFY ENDPOINT HIT ===");
|
||||
console.log("Verify endpoint - Strategy:", req.query.strategy);
|
||||
console.log("Verify endpoint - Authenticated:", req.isAuthenticated());
|
||||
console.log("Verify endpoint - User:", !!req.user);
|
||||
console.log("Verify endpoint - User ID:", req.user?.id);
|
||||
console.log("Verify endpoint - Auth error:", auth_error);
|
||||
console.log("Verify endpoint - Success message:", message);
|
||||
console.log("Verify endpoint - Flash errors:", errors);
|
||||
console.log("Verify endpoint - Flash messages:", messages);
|
||||
console.log("Verify endpoint - Session ID:", req.sessionID);
|
||||
console.log("Verify endpoint - Session passport:", (req.session as any).passport);
|
||||
console.log("Verify endpoint - Session flash:", (req.session as any).flash);
|
||||
|
||||
// Determine title based on authentication status and strategy
|
||||
let title = null;
|
||||
if (req.query.strategy) {
|
||||
if (auth_error) {
|
||||
// Show failure title only when there's an actual error
|
||||
title = req.query.strategy === "login" ? "Login Failed!" : "Signup Failed!";
|
||||
} else if (req.isAuthenticated() && message) {
|
||||
// Show success title when authenticated and there's a success message
|
||||
title = req.query.strategy === "login" ? "Login Successful!" : "Signup Successful!";
|
||||
}
|
||||
// If no error and not authenticated, don't show any title (this might be a redirect without completion)
|
||||
}
|
||||
|
||||
if (req.user)
|
||||
req.user.build_v = FileConstants.getRelease();
|
||||
|
||||
@@ -3,13 +3,23 @@ import { Strategy as LocalStrategy } from "passport-local";
|
||||
import { log_error } from "../../shared/utils";
|
||||
import db from "../../config/db";
|
||||
import { Request } from "express";
|
||||
import { ERROR_KEY, SUCCESS_KEY } from "./passport-constants";
|
||||
|
||||
async function handleLogin(req: Request, email: string, password: string, done: any) {
|
||||
console.log("=== LOGIN STRATEGY STARTED ===");
|
||||
console.log("Login attempt for:", email);
|
||||
console.log("Password provided:", !!password);
|
||||
console.log("Request body:", req.body);
|
||||
|
||||
// Clear any existing flash messages
|
||||
(req.session as any).flash = {};
|
||||
|
||||
if (!email || !password) {
|
||||
console.log("Missing credentials");
|
||||
return done(null, false, { message: "Please enter both email and password" });
|
||||
console.log("Missing credentials - email:", !!email, "password:", !!password);
|
||||
const errorMsg = "Please enter both email and password";
|
||||
console.log("Setting error flash message:", errorMsg);
|
||||
req.flash(ERROR_KEY, errorMsg);
|
||||
return done(null, false);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -24,18 +34,30 @@ async function handleLogin(req: Request, email: string, password: string, done:
|
||||
const [data] = result.rows;
|
||||
|
||||
if (!data?.password) {
|
||||
console.log("No account found");
|
||||
return done(null, false, { message: "No account found with this email" });
|
||||
console.log("No account found for email:", email);
|
||||
const errorMsg = "No account found with this email";
|
||||
console.log("Setting error flash message:", errorMsg);
|
||||
req.flash(ERROR_KEY, errorMsg);
|
||||
return done(null, false);
|
||||
}
|
||||
|
||||
const passwordMatch = bcrypt.compareSync(password, data.password);
|
||||
console.log("Password match:", passwordMatch);
|
||||
console.log("Password match result:", passwordMatch);
|
||||
|
||||
if (passwordMatch && email === data.email) {
|
||||
delete data.password;
|
||||
return done(null, data, {message: "User successfully logged in"});
|
||||
console.log("Login successful for user:", data.id);
|
||||
const successMsg = "User successfully logged in";
|
||||
console.log("Setting success flash message:", successMsg);
|
||||
req.flash(SUCCESS_KEY, successMsg);
|
||||
return done(null, data);
|
||||
}
|
||||
return done(null, false, { message: "Incorrect email or password" });
|
||||
|
||||
console.log("Password mismatch or email mismatch");
|
||||
const errorMsg = "Incorrect email or password";
|
||||
console.log("Setting error flash message:", errorMsg);
|
||||
req.flash(ERROR_KEY, errorMsg);
|
||||
return done(null, false);
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
log_error(error, req.body);
|
||||
|
||||
@@ -17,7 +17,19 @@ const options = (key: string): passport.AuthenticateOptions => ({
|
||||
successRedirect: `/secure/verify?strategy=${key}`
|
||||
});
|
||||
|
||||
authRouter.post("/login", passport.authenticate("local-login", options("login")));
|
||||
// Debug middleware for login
|
||||
const loginDebugMiddleware = (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
console.log("=== LOGIN ROUTE HIT ===");
|
||||
console.log("Request method:", req.method);
|
||||
console.log("Request URL:", req.url);
|
||||
console.log("Request body:", req.body);
|
||||
console.log("Content-Type:", req.headers["content-type"]);
|
||||
console.log("Session ID:", req.sessionID);
|
||||
console.log("Is authenticated before:", req.isAuthenticated());
|
||||
next();
|
||||
};
|
||||
|
||||
authRouter.post("/login", loginDebugMiddleware, passport.authenticate("local-login", options("login")));
|
||||
authRouter.post("/signup", signUpValidator, passwordValidator, passport.authenticate("local-signup", options("signup")));
|
||||
authRouter.post("/signup/check", signUpValidator, passwordValidator, safeControllerFunction(AuthController.status_check));
|
||||
authRouter.get("/verify", AuthController.verify);
|
||||
|
||||
Reference in New Issue
Block a user