8000 Outsourced password & email validation logic into seperate utility fu… · maciejkuran/forms-validation-API@aee0ba0 · GitHub
[go: up one dir, main page]

Skip to content

Commit aee0ba0

Browse files
committed
Outsourced password & email validation logic into seperate utility functions
1 parent 0e01805 commit aee0ba0

File tree

5 files changed

+115
-57
lines changed

5 files changed

+115
-57
lines changed

pages/api/email-address.tsx

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextApiRequest, NextApiResponse } from 'next';
22
import rateLimiterMiddleware from '@/rateLimitedMiddleware';
3+
import validateEmail from '@/utils/validateEmail';
34

45
const rateLimiter = {};
56

@@ -15,42 +16,9 @@ const handler = (req: NextApiRequest, res: NextApiResponse) => {
1516
return res.status(400).json({ error: 'Invalid request method. Accepted: POST' });
1617
}
1718

18-
// Check if email is not empty
19-
if (!email) {
20-
return res.status(400).json({ error: 'Email is required.' });
21-
}
22-
23-
// Check if email contains special characters
24-
const specialCharsRegex = /[!#$%^&*(),?":{}|<>~^+/=]/;
25-
if (specialCharsRegex.test(email)) {
26-
return res.status(400).json({ error: 'Email contains special characters.' });
27-
}
19+
const correctEmail = validateEmail(email, res);
2820

29-
// Check if email has no spaces
30-
if (/\s/.test(email)) {
31-
return res.status(400).json({ error: 'Email should not contain spaces.' });
32-
}
33-
34-
// Check if email contains @ symbol
35-
if (!/@/.test(email)) {
36-
return res.status(400).json({ error: 'Email should contain @ symbol.' });
37-
}
38-
39-
// Split email into username and domain parts
40-
const [username, domain] = email.split('@');
41-
42-
// Check if @ is not in the username portion
43-
if (/@/.test(username)) {
44-
return res.status(400).json({ error: 'Invalid email format.' });
45-
}
46-
47-
// Check if email contains offensive or inappropriate content
48-
//prettier-ignore
49-
const prohibitedWords = ['bitch', 'motherfucker', 'shit', 'pussy', 'ass', 'asshole', 'bollocks', 'fuck', 'cock', 'cocksucker', 'cunt', 'dick', 'crap', 'nigga', 'nigra', "nigger", 'slut', 'sonofabitch', 'whore', 'twat', 'moron', 'idiot', 'stupid' ]; // Feel free to add more words...
50-
const containsProhibitedWord = prohibitedWords.some(word => email.toLowerCase().includes(word));
51-
if (containsProhibitedWord) {
52-
return res.status(400).json({ error: 'Email contains offensive content.' });
53-
}
21+
if (!correctEmail) return;
5422

5523
// Email is valid
5624
return res.status(200).json({ message: 'Email is valid.' });

pages/api/password.tsx

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextApiRequest, NextApiResponse } from 'next';
22
import rateLimiterMiddleware from '@/rateLimitedMiddleware';
3+
import validatePassword from '@/utils/validatePassword';
34

45
const rateLimiter = {};
56

@@ -15,29 +16,9 @@ const handler = (req: NextApiRequest, res: NextApiResponse) => {
1516
return res.status(400).json({ error: 'Invalid request method. Accepted: POST' });
1617
}
1718

18-
if (!password) {
19-
return res.status(400).json({ error: 'Password input field cannot be empty.' });
20-
}
21-
22-
// Validating password length
23-
if (password.length < 8) {
24-
return res.status(400).json({ error: 'Password must contain at least 8 characters.' });
25-
}
26-
27-
// Checking if a password contains at least 1 digit
28-
if (!/\d/.test(password)) {
29-
return res.status(400).json({ error: 'Password must contain at least 1 digit. ' });
30-
}
31-
32-
// Checking if contains at least 1 capital letter
33-
if (!/[A-Z]/.test(password)) {
34-
return res.status(400).json({ error: 'Password must contain at least 1 capital letter.' });
35-
}
19+
const correctPassword = validatePassword(password, res);
3620

37-
// At least 1 special characters
38-
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
39-
return res.status(400).json({ error: 'Password must contain at least 1 special character.' });
40-
}
21+
if (!correctPassword) return;
4122

4223
return res.status(200).json({ success: 'Correct password.' });
4324
};

pages/api/sign-in.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { NextApiRequest, NextApiResponse } from 'next';
2+
import rateLimiterMiddleware from '@/rateLimitedMiddleware';
3+
4+
const rateLimiter = {};
5+
6+
const handler = (req: NextApiRequest, res: NextApiResponse) => {
7+
const { email } = req.body;
8+
9+
//Check rate limit
10+
const rateLimitOk = rateLimiterMiddleware(req, res, rateLimiter);
11+
12+
if (!rateLimitOk) return;
13+
14+
if (req.method !== 'POST') {
15+
return res.status(400).json({ error: 'Invalid request method. Accepted: POST' });
16+
}
17+
18+
//Validate email address
19+
20+
//Validate password
21+
};
22+
23+
export default handler;

utils/validateEmail.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDif F438 f line change
@@ -0,0 +1,50 @@
1+
import { NextApiResponse } from 'next';
2+
3+
const validateEmail = (email: string, res: NextApiResponse) => {
4+
// Check if email is not empty
5+
if (!email) {
6+
res.status(400).json({ error: 'Email is required.' });
7+
return false;
8+
}
9+
10+
// Check if email contains special characters
11+
const specialCharsRegex = /[!#$%^&*(),?":{}|<>~^+/=`]/;
12+
if (specialCharsRegex.test(email)) {
13+
res.status(400).json({ error: 'Email contains special characters.' });
14+
return false;
15+
}
16+
17+
// Check if email has no spaces
18+
if (/\s/.test(email)) {
19+
res.status(400).json({ error: 'Email should not contain spaces.' });
20+
return false;
21+
}
22+
23+
// Check if email contains @ symbol
24+
if (!/@/.test(email)) {
25+
res.status(400).json({ error: 'Email should contain @ symbol.' });
26+
return false;
27+
}
28+
29+
// Split email into username and domain parts
30+
const [username, domain] = email.split('@');
31+
32+
// Check if @ is not in the username portion
33+
if (/@/.test(username)) {
34+
res.status(400).json({ error: 'Invalid email format.' });
35+
return false;
36+
}
37+
38+
// Check if email contains offensive, vulgar or inappropriate content
39+
//prettier-ignore
40+
const prohibitedWords = ['bitch', 'motherfucker', 'shit', 'pussy', 'ass', 'asshole', 'bollocks', 'fuck', 'cock', 'cocksucker', 'cunt', 'dick', 'crap', 'nigga', 'nigra', "nigger", 'slut', 'sonofabitch', 'whore', 'twat', 'moron', 'idiot', 'stupid' ]; // Feel free to add more words...
41+
const containsProhibitedWord = prohibitedWords.some(word => email.toLowerCase().includes(word));
42+
if (containsProhibitedWord) {
43+
res.status(400).json({ error: 'Email contains offensive content.' });
44+
return false;
45+
}
46+
47+
return true;
48+
};
49+
50+
export default validateEmail;

utils/validatePassword.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { NextApiResponse } from 'next';
2+
3+
const validatePassword = (password: string, res: NextApiResponse) => {
4+
if (!password) {
5+
res.status(400).json({ error: 'Password input field cannot be empty.' });
6+
return false;
7+
}
8+
9+
// Validating password length
10+
if (password.length < 8) {
11+
res.status(400).json({ error: 'Password must contain at least 8 characters.' });
12+
return false;
13+
}
14+
15+
// Checking if a password contains at least 1 digit
16+
if (!/\d/.test(password)) {
17+
res.status(400).json({ error: 'Password must contain at least 1 digit. ' });
18+
return false;
19+
}
20+
21+
// Checking if contains at least 1 capital letter
22+
if (!/[A-Z]/.test(password)) {
23+
res.status(400).json({ error: 'Password must contain at least 1 capital letter.' });
24+
return false;
25+
}
26+
27+
// At least 1 special characters
28+
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
29+
res.status(400).json({ error: 'Password must contain at least 1 special character.' });
30+
return false;
31+
}
32+
33+
return true;
34+
};
35+
36+
export default validatePassword;

0 commit comments

Comments
 (0)
0