fix(auth): show clear message instead of error code on login failure

This commit is contained in:
Utkarsh-X 2025-09-18 16:35:41 +05:30
parent 40b5161a52
commit 779ec599a2
2 changed files with 13 additions and 3 deletions

View file

@ -88,10 +88,16 @@ class CustomBearerTransport(BearerTransport):
else: else:
return JSONResponse(model_dump(bearer_response)) return JSONResponse(model_dump(bearer_response))
async def get_login_failure_response(self) -> Response:
# 👇 This is the new part — gives a clear message instead of raw error code
return JSONResponse(
status_code=401,
content={"detail": "Wrong username or password"},
)
bearer_transport = CustomBearerTransport(tokenUrl="auth/jwt/login") bearer_transport = CustomBearerTransport(tokenUrl="auth/jwt/login")
auth_backend = AuthenticationBackend( auth_backend = AuthenticationBackend(
name="jwt", name="jwt",
transport=bearer_transport, transport=bearer_transport,

View file

@ -42,18 +42,22 @@ export function LocalLoginForm() {
const data = await response.json(); const data = await response.json();
if (!response.ok) { if (!response.ok) {
throw new Error(data.detail || "Failed to login"); // 👇 Instead of generic error, show backends message
setError(data.detail || "Wrong username or password");
return;
} }
router.push(`/auth/callback?token=${data.access_token}`); router.push(`/auth/callback?token=${data.access_token}`);
} catch (err) { } catch (err) {
const errorMessage = err instanceof Error ? err.message : "An error occurred during login"; const errorMessage =
err instanceof Error ? err.message : "An error occurred during login";
setError(errorMessage); setError(errorMessage);
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }
}; };
return ( return (
<div className="w-full max-w-md"> <div className="w-full max-w-md">
<form onSubmit={handleSubmit} className="space-y-4"> <form onSubmit={handleSubmit} className="space-y-4">