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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user