Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions routes/auth/signup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { COOKIE_MAX_AGE } from '../../server/constants.js';
import getUser from '../../database/account-info/get-user.js';
import createUser from '../../database/account-info/create-user.js';
import { generateToken, saltAndHashPassword, sendVerificationEmail, validateUsername } from '../../server/authentication.js';
import { generateToken, saltAndHashPassword, sendVerificationEmail, validateEmail, validateUsername } from '../../server/authentication.js';

import { Router } from 'express';

Expand Down Expand Up @@ -30,8 +30,13 @@ router.post('/', async (req, res) => {
req.session.token = generateToken(username);
req.session.expires = expires;

const password = saltAndHashPassword(req.body.password);
const email = req.body.email;
if (email && !validateEmail(email)) {
res.sendStatus(400);
return;
}

const password = saltAndHashPassword(req.body.password);
await createUser(username, password, email);
sendVerificationEmail(username);
// console.log(`/api/auth: SIGNUP: User ${username} successfully signed up.`);
Expand Down
9 changes: 9 additions & 0 deletions server/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ export function updatePassword (username, newPassword) {
return updateUser(username, { password: saltAndHashPassword(newPassword) });
}

/**
*
* @param {string} email
* @returns {boolean} True if the email is valid, and false otherwise.
*/
export function validateEmail (email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}

/**
*
* @param {string} username
Expand Down
Loading