BETTERZON-75: Adding regex to check email and username

This commit is contained in:
Patrick Müller 2021-04-28 22:16:16 +02:00
parent 820decd8f0
commit 94b02d90d7
2 changed files with 17 additions and 2 deletions

View File

@ -21,7 +21,6 @@ export const usersRouter = express.Router();
*/
// POST users/register
usersRouter.post('/register', async (req: Request, res: Response) => {
try {
const username: string = req.body.username;

View File

@ -58,7 +58,7 @@ export const createUser = async (username: string, password: string, email: stri
await conn.commit();
// Get session id of the created user
let sessionId: number = -1
let sessionId: number = -1;
for (const row in sessionIdRes) {
if (row !== 'meta' && sessionIdRes[row].session_id != null) {
sessionId = sessionIdRes[row].session_id;
@ -113,6 +113,22 @@ export const checkUsernameAndEmail = async (username: string, email: string): Pr
codes: []
};
const usernameRegex = RegExp('^[a-zA-Z0-9\\-\\_]{4,20}$'); // Can contain a-z, A-Z, 0-9, -, _ and has to be 4-20 chars long
if (!usernameRegex.test(username)) {
// Username doesn't match requirements
res.hasProblems = true;
res.messages.push('Invalid username');
res.codes.push(1);
}
const emailRegex = RegExp('^[a-zA-Z0-9\\-\\_.]{1,30}\\@[a-zA-Z0-9\\-.]{1,20}\\.[a-z]{1,20}$'); // Normal email regex, user@betterzon.xyz
if (!emailRegex.test(email)) {
// Username doesn't match requirements
res.hasProblems = true;
res.messages.push('Invalid email');
res.codes.push(2);
}
if (usernameRes.length > 0) {
// Username is a duplicate
res.hasProblems = true;