Skip to content

Commit

Permalink
Adding status codes to validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hgorges committed Aug 24, 2024
1 parent 8c19f5c commit 430153d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/controller/loginPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export async function renderLogin(
res: Response,
_next: NextFunction,
renderOptions: {
statusCode?: number;
username?: string;
password?: string;
} = {},
Expand All @@ -17,7 +18,7 @@ export async function renderLogin(
}
const infoMessage = req.flash('info');
const errorMessage = req.flash('error');
res.render('login', {
res.status(renderOptions.statusCode ?? 200).render('login', {
csrfToken: res.locals.csrfToken,
infoMessage: infoMessage.length > 0 ? infoMessage[0] : null,
errorMessage: errorMessage.length > 0 ? errorMessage[0] : null,
Expand All @@ -35,14 +36,14 @@ export async function login(
const { username, password } = req.body;

if (username.toLowerCase() === 'system') {
renderLogin(req, res, next, req.body);
renderLogin(req, res, next, { statusCode: 422, ...req.body });
return;
}

const user = await userModel.getUserForLogin(username, password);
if (!user) {
req.flash('error', 'Invalid username or password!');
renderLogin(req, res, next, req.body);
renderLogin(req, res, next, { statusCode: 422, ...req.body });
return;
}

Expand Down
5 changes: 3 additions & 2 deletions src/controller/passwordChangePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export async function renderPasswordChange(
res: Response,
_next: NextFunction,
renderOptions: {
statusCode?: number;
password?: string;
confirm_password?: string;
} = {},
Expand All @@ -25,7 +26,7 @@ export async function renderPasswordChange(
const infoMessage = req.flash('info');
const errorMessage = req.flash('error');

res.render('password-change', {
res.status(renderOptions.statusCode ?? 200).render('password-change', {
csrfToken: res.locals.csrfToken,
userId: user.user_id,
passwordResetToken: req.params.passwordResetToken,
Expand All @@ -48,7 +49,7 @@ export async function changePassword(
if (password !== confirm_password) {
req.flash('error', 'Passwords do not match!');
req.params.passwordResetToken = passwordResetToken;
renderPasswordChange(req, res, next, req.body);
renderPasswordChange(req, res, next, { statusCode: 422, ...req.body });
return;
}

Expand Down
7 changes: 5 additions & 2 deletions src/controller/passwordResetPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export async function renderPasswordReset(
req: Request,
res: Response,
_next: NextFunction,
renderOptions: { email?: string } = {},
renderOptions: {
statusCode?: number;
email?: string;
} = {},
): Promise<void> {
if (req.session.isAuthenticated) {
res.redirect('/');
Expand All @@ -17,7 +20,7 @@ export async function renderPasswordReset(
const infoMessage = req.flash('info');
const errorMessage = req.flash('error');

res.render('password-reset', {
res.status(renderOptions.statusCode ?? 200).render('password-reset', {
csrfToken: res.locals.csrfToken,
infoMessage: infoMessage.length > 0 ? infoMessage[0] : null,
errorMessage: errorMessage.length > 0 ? errorMessage[0] : null,
Expand Down
7 changes: 4 additions & 3 deletions src/controller/settingsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export async function renderSettingsPage(
res: Response,
_next: NextFunction,
renderOptions: {
statusCode?: number;
username?: string;
first_name?: string;
last_name?: string;
Expand All @@ -26,7 +27,7 @@ export async function renderSettingsPage(
const infoMessage = req.flash('info');
const errorMessage = req.flash('error');

res.render('settings', {
res.status(renderOptions.statusCode ?? 200).render('settings', {
path: '/settings',
csrfToken: res.locals.csrfToken,
isAdmin: req.session.isAdmin,
Expand Down Expand Up @@ -66,13 +67,13 @@ export async function saveSettings(

if (password !== confirm_password) {
req.flash('error', 'Passwords do not match!');
renderSettingsPage(req, res, next, req.body);
renderSettingsPage(req, res, next, { statusCode: 422, ...req.body });
return;
}

if (password == null || password.length <= 7) {
req.flash('error', 'Password must be at least 8 characters long!');
renderSettingsPage(req, res, next, req.body);
renderSettingsPage(req, res, next, { statusCode: 422, ...req.body });
return;
}

Expand Down
11 changes: 6 additions & 5 deletions src/controller/signupPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export async function renderSignup(
res: Response,
_next: NextFunction,
renderOptions: {
statusCode?: number;
username?: string;
first_name?: string;
last_name?: string;
Expand All @@ -22,7 +23,7 @@ export async function renderSignup(
return;
}
const errorMessage = req.flash('error');
res.render('signup', {
res.status(renderOptions.statusCode ?? 200).render('signup', {
csrfToken: res.locals.csrfToken,
errorMessage: errorMessage.length > 0 ? errorMessage[0] : null,
username: '',
Expand Down Expand Up @@ -52,23 +53,23 @@ export async function signup(
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!emailRegex.test(email)) {
req.flash('error', 'Valid email is required!');
renderSignup(req, res, next, req.body);
renderSignup(req, res, next, { statusCode: 422, ...req.body });
return;
}

if (await userModel.getUserByUsername(username)) {
req.flash('error', 'Username is already taken!');
renderSignup(req, res, next, req.body);
renderSignup(req, res, next, { statusCode: 422, ...req.body });
return;
}
if (await userModel.getUserByEmail(email)) {
req.flash('error', 'Email is already taken!');
renderSignup(req, res, next, req.body);
renderSignup(req, res, next, { statusCode: 422, ...req.body });
return;
}
if (password !== confirm_password) {
req.flash('error', 'Passwords do not match!');
renderSignup(req, res, next, req.body);
renderSignup(req, res, next, { statusCode: 422, ...req.body });
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function validate(
if (!isValid && validateFunction.errors) {
const error = parseErrors(validateFunction.errors);
req.flash('error', error.map((e) => e.message).join(', '));
renderFunction(req, res, next, req.body);
renderFunction(req, res, next, { statusCode: 422, ...req.body });
return;
}
next();
Expand Down

0 comments on commit 430153d

Please sign in to comment.