From c7c0830780c9a2f31dd100fd0c4e2a24ba1fba94 Mon Sep 17 00:00:00 2001 From: marco370 <48531002-marco370@users.noreply.replit.com> Date: Thu, 23 Oct 2025 15:21:02 +0000 Subject: [PATCH] Improve local login handling with Passport.js authentication Update the `/api/local-login` route to use Passport.js middleware for robust local authentication, including error handling for authentication failures and successful login. Replit-Commit-Author: Agent Replit-Commit-Session-Id: e5565357-90e1-419f-b9a8-6ee8394636df Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e5565357-90e1-419f-b9a8-6ee8394636df/WbUtQAg --- server/localAuth.ts | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/server/localAuth.ts b/server/localAuth.ts index bae7ff5..e8fffc9 100644 --- a/server/localAuth.ts +++ b/server/localAuth.ts @@ -138,12 +138,37 @@ export async function setupLocalAuth(app: Express) { }); // Route login locale POST - app.post("/api/local-login", passport.authenticate("local"), (req, res) => { - res.json({ - success: true, - user: req.user, - message: "Login effettuato con successo" - }); + app.post("/api/local-login", (req, res, next) => { + passport.authenticate("local", (err: any, user: any, info: any) => { + if (err) { + return res.status(500).json({ + success: false, + message: "Errore durante il login" + }); + } + + if (!user) { + return res.status(401).json({ + success: false, + message: info?.message || "Email o password non corretti" + }); + } + + req.login(user, (loginErr) => { + if (loginErr) { + return res.status(500).json({ + success: false, + message: "Errore durante il login" + }); + } + + return res.json({ + success: true, + user: req.user, + message: "Login effettuato con successo" + }); + }); + })(req, res, next); });