Skip to content

Commit 834d079

Browse files
committed
Fixed registration bug allowing invalid passwords
1 parent 485000e commit 834d079

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

app/(api)/api/auth/login/route.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { NextRequest, NextResponse } from 'next/server';
44

55
import Login from '@datalib/auth/login';
66

7+
/*
8+
I created a custom API route to log in because the default route provided by Auth.js
9+
requires you to first get your CSRF token before logging in, this takes care of it
10+
*/
711
export async function POST(request: NextRequest) {
812
const body = await request.json();
913
const res = await Login(body.email, body.password);

app/(pages)/(hackers)/login/_components/LoginForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function LoginForm() {
3434

3535
const validateForm = (email: string, password: string) => {
3636
const isEmailValid = /\S+@\S+\.\S+/.test(email);
37-
const isPasswordValid = password.length >= 6;
37+
const isPasswordValid = password.length >= 6 && password.length <= 20;
3838
setValid(isEmailValid && isPasswordValid);
3939
};
4040

app/(pages)/(hackers)/register/_components/RegisterForm.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,29 @@ export default function RegisterForm() {
5858
setPasswordError(!isEmailValid);
5959

6060
const isPasswordValid =
61-
password.length >= 6 || password.length <= 20 || password.length === 0;
61+
(password.length >= 6 && password.length <= 20) || password.length === 0;
6262
if (!isPasswordValid) {
6363
setError('Password is too short.');
6464
}
6565
setPasswordError(!isPasswordValid);
6666

67-
const passwordMatch =
68-
password === passwordDupe || passwordDupe.length === 0;
67+
const passwordMatch = password === passwordDupe;
6968
if (!passwordMatch) {
7069
setError("Passwords don't match.");
7170
}
7271
setPasswordDupeError(!passwordMatch);
7372

74-
setValid(isEmailValid && isPasswordValid && passwordMatch);
7573
if (
7674
email.length === 0 ||
7775
password.length === 0 ||
7876
passwordDupe.length === 0
7977
) {
8078
setValid(false);
8179
}
80+
8281
if (isEmailValid && isPasswordValid && passwordMatch) {
8382
setError('');
83+
setValid(true);
8484
}
8585
};
8686

app/(pages)/judges/register/_components/RegisterForm.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,18 @@ export default function RegisterForm() {
5858
setPasswordError(!isEmailValid);
5959

6060
const isPasswordValid =
61-
password.length >= 6 || password.length <= 20 || password.length === 0;
61+
(password.length >= 6 && password.length <= 20) || password.length === 0;
6262
if (!isPasswordValid) {
6363
setError('Password is too short.');
6464
}
6565
setPasswordError(!isPasswordValid);
6666

67-
const passwordMatch =
68-
password === passwordDupe || passwordDupe.length === 0;
67+
const passwordMatch = password === passwordDupe;
6968
if (!passwordMatch) {
7069
setError("Passwords don't match.");
7170
}
7271
setPasswordDupeError(!passwordMatch);
7372

74-
setValid(isEmailValid && isPasswordValid && passwordMatch);
7573
if (
7674
email.length === 0 ||
7775
password.length === 0 ||
@@ -81,6 +79,7 @@ export default function RegisterForm() {
8179
}
8280
if (isEmailValid && isPasswordValid && passwordMatch) {
8381
setError('');
82+
setValid(true);
8483
}
8584
};
8685

0 commit comments

Comments
 (0)