You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
979 B
JavaScript
39 lines
979 B
JavaScript
const router = require('express-promise-router')()
|
|
const parseJSON = require('body-parser').json()
|
|
const db = require('../db')
|
|
|
|
const {validationResult} = require('express-validator')
|
|
const validate = require('./middleware/validators')
|
|
|
|
const registerValidation = [validate.emailRestrictions, validate.passwordRestrictions, validate.bothPasswordsMatch, validate.checkEmailNotUsed]
|
|
router.post('/', parseJSON, registerValidation, async (req, res) => {
|
|
const errors = validationResult(req)
|
|
|
|
if(!errors.isEmpty())
|
|
return res.status(422).json({errors: errors.array()})
|
|
|
|
const user = await db.user.register(
|
|
req.body.email,
|
|
req.body.password
|
|
)
|
|
|
|
if(!user){
|
|
return res.status(422).json({errors: [{
|
|
param: 'email',
|
|
msg: 'Unable to complete registration'
|
|
},{
|
|
param: 'password',
|
|
msg: ' '
|
|
},{
|
|
param: 'password2',
|
|
msg: ' '
|
|
}]})
|
|
}
|
|
|
|
await db.session.create(req, user)
|
|
|
|
res.json(user)
|
|
})
|
|
|
|
module.exports = router;
|