-
Notifications
You must be signed in to change notification settings - Fork 8
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
16 changed files
with
365 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { Router } from 'express'; | ||
import authController from '../controllers/auth'; | ||
import validateBody from '../../middleware/userValidations'; | ||
import userValidation from '../../middleware/validUser'; | ||
|
||
|
||
const authRouter = Router(); | ||
const { signup } = authController; | ||
|
||
authRouter.post('/signup', signup); | ||
authRouter.post('/signup', validateBody('signup'), userValidation.usernameExists, userValidation.emailExists, signup); | ||
|
||
export default authRouter; |
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
File renamed without changes.
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,47 @@ | ||
import Joi from '@hapi/joi'; | ||
|
||
export default { | ||
signup: Joi.object().keys({ | ||
firstName: Joi.string() | ||
.trim() | ||
.required() | ||
.regex(/^[A-Za-z_-]+$/) | ||
.min(3) | ||
.label('First name is required, it must have at least 3 letters and must contain only letters, underscores(_) and hyphens (-)'), | ||
lastName: Joi.string() | ||
.trim() | ||
.required() | ||
.regex(/^[A-Za-z_.-]+$/) | ||
.min(3) | ||
.label('Last name is required, it must have at least 3 letters and must contain only letters, underscores(_) and hyphens (-)'), | ||
username: Joi.string() | ||
.trim() | ||
.lowercase() | ||
.required() | ||
.regex(/^[a-zA-Z0-9_.-]+$/) | ||
.min(3) | ||
.label('Username is required, it must have at least 3 letters and must contain only letters, numbers, underscores(_), hyphens (-) and points (.)'), | ||
email: Joi.string() | ||
.trim() | ||
.lowercase() | ||
.email() | ||
.required() | ||
.label('Email is required and should look like this : example@email.com!'), | ||
password: Joi.string() | ||
.trim() | ||
.regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})/) | ||
.required() | ||
.label('Password is required and must be at least 8 letters containing' | ||
+ ' at least a number a Lowercase letter and an Uppercase letter'), | ||
confirmPassword: Joi.any() | ||
.required() | ||
.valid(Joi.ref('password')) | ||
.label('Password and Confirm Password do not match'), | ||
bio: Joi.string(), | ||
image: Joi.string(), | ||
dateOfBirth: Joi.string(), | ||
gender: Joi.string(), | ||
socialId: Joi.string(), | ||
provider: Joi.string() | ||
}), | ||
}; |
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 @@ | ||
import Joi from '@hapi/joi'; | ||
import _ from 'lodash'; | ||
import Schemas from '../helpers/userSchema'; | ||
|
||
const validateUser = schema => (req, res, next) => { | ||
const data = req.body; | ||
|
||
if (_.has(Schemas, schema)) { | ||
const chosenSchema = _.get(Schemas, schema); | ||
const err = Joi.validate(data, chosenSchema, { abortEarly: false }); | ||
|
||
if (!err.error) { | ||
req.body = data; | ||
next(); | ||
} else { | ||
const allErrors = []; | ||
err.error.details.forEach((errors) => { | ||
const findError = allErrors.filter(error => error === errors.context.label); | ||
if (findError.length === 0) { | ||
allErrors.push(errors.context.label); | ||
} | ||
}); | ||
return res.status(400).send({ | ||
message: allErrors, | ||
}); | ||
} | ||
} | ||
next(); | ||
}; | ||
|
||
export default validateUser; |
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,41 @@ | ||
import models from '../sequelize/models'; | ||
|
||
const validUser = { | ||
async emailExists(req, res, next) { | ||
const { email } = req.body; | ||
await models.User.findAll({ | ||
where: { | ||
}, | ||
}).then((data) => { | ||
if (data.length > 0) { | ||
return res.status(400).json({ | ||
status: 400, | ||
message: 'This email is already in use', | ||
}); | ||
} | ||
}); | ||
|
||
next(); | ||
}, | ||
async usernameExists(req, res, next) { | ||
const { username } = req.body; | ||
await models.User.findAll({ | ||
where: { | ||
username | ||
}, | ||
}).then((data) => { | ||
if (data.length > 0) { | ||
return res.status(400).json({ | ||
status: 400, | ||
message: 'This username is not available, Please choose another one!', | ||
}); | ||
} | ||
}); | ||
|
||
next(); | ||
}, | ||
}; | ||
|
||
|
||
export default validUser; |
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,60 @@ | ||
|
||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
firstName: { | ||
type: Sequelize.STRING | ||
}, | ||
lastName: { | ||
type: Sequelize.STRING | ||
}, | ||
username: { | ||
type: Sequelize.STRING | ||
}, | ||
email: { | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
type: Sequelize.STRING | ||
}, | ||
bio: { | ||
type: Sequelize.TEXT | ||
}, | ||
image: { | ||
type: Sequelize.TEXT | ||
}, | ||
dateOfBirth: { | ||
type: Sequelize.DATE | ||
}, | ||
gender: { | ||
type: Sequelize.STRING | ||
}, | ||
provider: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
socialId: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
verified: { | ||
allowNull: false, | ||
type: Sequelize.BOOLEAN | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Users') | ||
}; |
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,17 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const User = sequelize.define('User', { | ||
firstName: DataTypes.STRING, | ||
lastName: DataTypes.STRING, | ||
username: DataTypes.STRING, | ||
email: DataTypes.STRING, | ||
password: DataTypes.STRING, | ||
bio: DataTypes.TEXT, | ||
image: DataTypes.TEXT, | ||
dateOfBirth: DataTypes.DATE, | ||
gender: DataTypes.STRING, | ||
provider: DataTypes.STRING, | ||
socialId: DataTypes.STRING, | ||
verified: DataTypes.BOOLEAN | ||
}, {}); | ||
return User; | ||
}; |
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,26 @@ | ||
|
||
|
||
module.exports = { | ||
up: queryInterface => | ||
/* | ||
Add altering commands here. | ||
Return a promise to correctly handle asynchronicity. | ||
*/ | ||
|
||
queryInterface.bulkInsert('Users', [{ | ||
firstName: 'Mireille', | ||
lastName: 'Niwemuhuza', | ||
username: 'mifeille', | ||
email: 'nimilleer@gmail.com', | ||
password: 'Mireille1!', | ||
bio: '', | ||
image: '', | ||
dateOfBirth: '12/12/2000', | ||
gender: '', | ||
provider: '', | ||
socialId: '', | ||
verified: 'false', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}], {}), | ||
}; |
Oops, something went wrong.