Skip to content

Commit

Permalink
moved vali to another file and fixed password req
Browse files Browse the repository at this point in the history
  • Loading branch information
gubbih committed Nov 14, 2023
1 parent 62f95ed commit ecb9906
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
18 changes: 18 additions & 0 deletions Src/errorHandler/validations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { User } from '../models/user';
import { body } from 'express-validator';

export function isValidUser(user: User): boolean {
if (user.password) {
Expand Down Expand Up @@ -34,3 +35,20 @@ export function isValidPassword(password: string): boolean {
const passwordRegex = /^(?=.*\d)(?=.*[!@#$%^&*])(?=.*[a-zæøå)(?=.*[A-ZÆØÅ]).{8,}$/;
return passwordRegex.test(password);
}

export const userValidationRules = [
body('firstName').notEmpty().withMessage('firstName is required'),
body('lastName').notEmpty().withMessage('firstName is required'),
body('email').notEmpty().withMessage('email is required'),
body('password').notEmpty().withMessage('password is required'),
];
export const userLoginValidationRules = [
body('email').notEmpty().withMessage('email is required'),
body('password').notEmpty().withMessage('password is required'),
];

export const eventValidationRules = [
body('title').notEmpty().withMessage('Title is required'),
body('start').isISO8601().toDate().withMessage('Must have a time and date'),
body('end').isISO8601().toDate().withMessage('Must have a time and date'),
];
9 changes: 3 additions & 6 deletions Src/routes/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router, Request, Response } from 'express';
import { body, validationResult } from 'express-validator';
import { validationResult } from 'express-validator';
import { Event } from '../models/event';
import { asyncHandler } from '../errorHandler/asyncHandler';
import {
Expand All @@ -9,14 +9,11 @@ import {
deleteEvent,
updateEvent,
} from '../firebase/events';
import { eventValidationRules } from '../errorHandler/validations';

const router = Router();

const eventValidationRules = [
body('title').notEmpty().withMessage('Title is required'),
body('start').isISO8601().toDate().withMessage('Must have a time and date'),
body('end').isISO8601().toDate().withMessage('Must have a time and date'),
];


// create new
router.post(
Expand Down
10 changes: 3 additions & 7 deletions Src/routes/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router, Request, Response } from 'express';

import { body, validationResult } from 'express-validator';
import { validationResult } from 'express-validator';
import { User } from '../models/user';
import { asyncHandler } from '../errorHandler/asyncHandler';
import {
Expand All @@ -14,15 +14,10 @@ import {
} from '../firebase/users';
import { auth, CustomRequest } from './auth';
import bcrypt from 'bcrypt';
import { userValidationRules, userLoginValidationRules } from '../errorHandler/validations';

const router = Router();

const userValidationRules = [
body('firstName').notEmpty().withMessage('firstName is required'),
body('lastName').notEmpty().withMessage('firstName is required'),
body('email').notEmpty().withMessage('email is required'),
body('password').notEmpty().withMessage('password is required'),
];
// create user
router.post(
'/:orgId/',
Expand Down Expand Up @@ -51,6 +46,7 @@ router.post(
//user logs in
router.post(
`/:orgId/login`,
userLoginValidationRules,
asyncHandler(async (req: Request, res: Response) => {
const user: { user: User } = await userLogin(
req.body.email,
Expand Down

0 comments on commit ecb9906

Please sign in to comment.