-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor login form and improve error handling #37
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,17 +121,41 @@ export default function Vote() { | |
.catch((error) => { | ||
setSending(false); | ||
if (error.code === "permission-denied") { | ||
alert( | ||
"Da hat etwas nicht geklappt. Sie sind nicht (mehr) berechtigt, Ihre Daten abzugeben. Bitte wenden Sie sich an den zuständigen Lehrer." | ||
); | ||
snackbar({ | ||
message: | ||
"Es ist ein Berechtigungsfehler aufgetreten.", | ||
action:"Details", | ||
onActionClick: () => { | ||
console.error(error); | ||
alert( | ||
"Es scheint, als sei die Wahl nicht mehr verfügbar. Bitte versuchen Sie es später erneut.\n"+error | ||
) | ||
} | ||
}); | ||
} else if (error.message === "Network Error") { | ||
alert( | ||
"Netzwerkprobleme. Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut." | ||
); | ||
snackbar({ | ||
message: | ||
"Es ist ein Netzwerkfehler aufgetreten.", | ||
action:"Details", | ||
onActionClick: () => { | ||
console.error(error); | ||
alert( | ||
"Es scheint, als gäbe es ein Problem mit Ihrer Internetverbindung. Bitte überprüfen Sie diese und versuchen Sie es erneut.\n"+error | ||
) | ||
} | ||
Comment on lines
+141
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid concatenating raw error objects in user alerts Similar to the previous issue, ensure that you use Apply this diff: onActionClick: () => {
console.error(error);
alert(
- "Es scheint, als gäbe es ein Problem mit Ihrer Internetverbindung. Bitte überprüfen Sie diese und versuchen Sie es erneut.\n" + error
+ "Es scheint, als gäbe es ein Problem mit Ihrer Internetverbindung. Bitte überprüfen Sie diese und versuchen Sie es erneut.\n" + error.message
);
}
|
||
}); | ||
} else { | ||
alert( | ||
"Ein unbekannter Fehler ist aufgetreten. Bitte versuchen Sie es später erneut." | ||
); | ||
snackbar({ | ||
message: | ||
"Es ist ein Fehler aufgetreten.", | ||
action:"Details", | ||
onActionClick: () => { | ||
console.error(error); | ||
alert( | ||
"Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später erneut.\n"+error | ||
) | ||
} | ||
Comment on lines
+153
to
+157
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid concatenating raw error objects in user alerts Again, to maintain consistency and security, use Apply this diff: onActionClick: () => {
console.error(error);
alert(
- "Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später erneut.\n" + error
+ "Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später erneut.\n" + error.message
);
}
|
||
}); | ||
} | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,43 @@ import { | |
import { alert, prompt, snackbar } from "mdui"; | ||
import React from "react"; | ||
import { auth } from "../../firebase"; | ||
import "../../vote.css"; | ||
|
||
export default function Login() { | ||
const [email, setEmail] = React.useState(""); | ||
const [password, setPassword] = React.useState(""); | ||
const [loading, setLoading] = React.useState(false); | ||
interface LoginFormElements extends HTMLFormControlsCollection { | ||
email: HTMLInputElement; | ||
password: HTMLInputElement; | ||
} | ||
|
||
interface LoginForm extends HTMLFormElement { | ||
readonly elements: LoginFormElements; | ||
} | ||
|
||
const handleLogin = (e: React.FormEvent<LoginForm>) => { | ||
e.preventDefault(); | ||
|
||
const formData = new FormData(e.currentTarget); | ||
const email = formData.get("email") as string; | ||
const password = formData.get("password") as string; | ||
Comment on lines
+25
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure The Apply this diff to add null checks: const email = formData.get("email") as string;
const password = formData.get("password") as string;
+if (!email || !password) {
+ snackbar({ message: "Bitte geben Sie Ihre Email und Ihr Passwort ein." });
+ setLoading(false);
+ return;
+}
|
||
|
||
setLoading(true); | ||
|
||
const handleLogin = () => { | ||
signInWithEmailAndPassword(auth, email, password) | ||
.then((userCredential) => {}) | ||
.then((userCredential) => { | ||
const user = userCredential.user; | ||
snackbar({ | ||
message: `Willkommen, ${user.email}`, | ||
closeable: true, | ||
}); | ||
setLoading(false); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
snackbar({ | ||
message: error.message, | ||
}); | ||
setLoading(false); | ||
JohanGrims marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}; | ||
|
||
|
@@ -33,47 +58,80 @@ export default function Login() { | |
label: "Email", | ||
}, | ||
onConfirm: (email) => { | ||
setLoading(true); | ||
sendPasswordResetEmail(auth, email) | ||
.then(() => { | ||
alert({ | ||
headline: "Email gesendet", | ||
description: "Bitte überprüfen Sie Ihren Posteingang.", | ||
}); | ||
setLoading(false); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
alert({ | ||
headline: "Fehler", | ||
description: error.message, | ||
}); | ||
setLoading(false); | ||
Comment on lines
+61
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Validate email input before sending password reset email Ensure that the email provided is not empty to prevent errors when sending the password reset email. This enhances user experience and avoids unnecessary API calls. Apply this diff to add an email validation check: onConfirm: (email) => {
+ if (!email) {
+ alert({
+ headline: "Email erforderlich",
+ description: "Bitte geben Sie Ihre Email-Adresse ein.",
+ });
+ return;
+ }
setLoading(true);
sendPasswordResetEmail(auth, email)
|
||
}); | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<mdui-card variant="filled" class="card"> | ||
<mdui-card variant="filled" class="card" style={{ position: "absolute" }}> | ||
{loading && ( | ||
<mdui-linear-progress | ||
style={{ | ||
position: "absolute", | ||
top: 0, | ||
left: 0, | ||
right: 0, | ||
zIndex: 100, | ||
}} | ||
/> | ||
)} | ||
<div className="mdui-prose"> | ||
<h1>Administratoren-Bereich</h1> | ||
<mdui-text-field | ||
value={email} | ||
onInput={(e) => setEmail(e.target.value)} | ||
type="email" | ||
placeholder="user@example.com" | ||
label="Email" | ||
></mdui-text-field> | ||
<p /> | ||
<mdui-text-field | ||
value={password} | ||
onInput={(e) => setPassword(e.target.value)} | ||
type="password" | ||
label="Passwort" | ||
toggle-password | ||
/> | ||
<p /> | ||
<br /> | ||
<div className="button-container"> | ||
<mdui-button variant="text" onClick={handlePasswordReset}> | ||
Passwort zurücksetzen | ||
</mdui-button> | ||
<mdui-button onClick={handleLogin}>Login</mdui-button> | ||
</div> | ||
<form onSubmit={handleLogin}> | ||
<mdui-text-field | ||
type="email" | ||
placeholder="user@example.com" | ||
label="Email" | ||
name="email" | ||
required | ||
></mdui-text-field> | ||
<p /> | ||
<mdui-text-field | ||
type="password" | ||
label="Passwort" | ||
toggle-password | ||
name="password" | ||
required | ||
/> | ||
<p /> | ||
<br /> | ||
<div className="button-container"> | ||
{loading ? ( | ||
<> | ||
<mdui-button variant="text" disabled> | ||
Passwort zurücksetzen | ||
</mdui-button> | ||
|
||
<mdui-button disabled>Login</mdui-button> | ||
</> | ||
) : ( | ||
<> | ||
<mdui-button variant="text" onClick={handlePasswordReset}> | ||
Passwort zurücksetzen | ||
</mdui-button> | ||
|
||
<mdui-button type="submit">Login</mdui-button> | ||
</> | ||
)} | ||
</div> | ||
</form> | ||
</div> | ||
</mdui-card> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid concatenating raw error objects in user alerts
Concatenating the
error
object directly in the alert message may not display meaningful information and can expose sensitive data. Useerror.message
to provide a user-friendly error description.Apply this diff to improve the error message: