-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
310 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import models from '../database/models'; | ||
import Response from '../utils/response'; | ||
|
||
const { User } = models; | ||
|
||
export default class UserMiddleware { | ||
static async verifyExistingUser(req, res, next) { | ||
try { | ||
const { email } = req.body; | ||
|
||
const user = await User.findOne({ where: { email } }); | ||
if (user) { | ||
const response = new Response( | ||
'Conflict', | ||
409, | ||
'User with email already registered' | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
next(); | ||
} catch (error) { | ||
const response = new Response( | ||
'Internal server error', | ||
500, | ||
`${error}`, | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import { validationResult } from 'express-validator'; | ||
import Response from '../utils/response'; | ||
|
||
export default class SignupValidator { | ||
static validateSignup(req, res, next) { | ||
const errors = validationResult(req); | ||
const validationErrors = []; | ||
|
||
if (errors) { | ||
errors.map(error => validationErrors.push(error.msg)); | ||
const response = new Response( | ||
'Bad request', | ||
400, | ||
'Invalid credentials', | ||
validationErrors | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @function capitalize | ||
* @description | ||
* @param {string} word | ||
* @return {function} call | ||
*/ | ||
const capitalize = (word) => { | ||
const formatWord = word | ||
.toLowerCase() | ||
.split(' ') | ||
.map((f) => f.charAt(0).toUpperCase() + f.substring(1)) | ||
.join(' '); | ||
|
||
return formatWord; | ||
}; | ||
|
||
export default capitalize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint-disable require-jsdoc */ | ||
class Response { | ||
constructor(status, code, messages, data) { | ||
this.status = status; | ||
this.code = code; | ||
this.messages = messages; | ||
this.data = data; | ||
} | ||
} | ||
|
||
export default Response; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import signup from './signup'; | ||
|
||
export default { | ||
signup, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { check } from 'express-validator/check'; | ||
import capitalize from '../utils/capitalize'; | ||
|
||
export default { | ||
signupSchema: [ | ||
check('firstName') | ||
.trim() | ||
.exists().withMessage('First name is required') | ||
.isLength({ min: 2, max: 15 }) | ||
.withMessage('First name should be between 2 to 15 characters') | ||
.isAlpha() | ||
.withMessage('First name should only be alphabets') | ||
.customSanitizer(value => capitalize(value)), | ||
|
||
check('lastName') | ||
.trim() | ||
.exists().withMessage('Last name is required') | ||
.isLength({ min: 2, max: 15 }) | ||
.withMessage('Last name should be between 2 to 15 characters') | ||
.isAlpha() | ||
.withMessage('Last name should only be alphabets') | ||
.customSanitizer(value => capitalize(value)), | ||
|
||
check('email') | ||
.trim() | ||
.exists().withMessage('Email address is required') | ||
.isEmail() | ||
.withMessage('Email address is invalid'), | ||
|
||
check('password') | ||
.trim() | ||
.exists().withMessage('Password is required') | ||
.isLength({ min: 8, }) | ||
.withMessage('Password must be alphanumeric and not less than 8 characters') | ||
.isAlpha() | ||
.withMessage('Password must be alphanumeric and not less than 8 characters') | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.