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.
35 lines
882 B
JavaScript
35 lines
882 B
JavaScript
5 years ago
|
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 loginValidation = [validate.emailRestrictions, validate.passwordRestrictions]
|
||
|
router.post('/', parseJSON, loginValidation, async (req, res) => {
|
||
|
const errors = validationResult(req)
|
||
|
|
||
|
if(!errors.isEmpty())
|
||
|
return res.status(422).json({errors: errors.array()})
|
||
|
|
||
|
const user = await db.user.login(
|
||
|
req.body.email,
|
||
|
req.body.password
|
||
|
)
|
||
|
|
||
|
// TODO: Create session
|
||
|
|
||
|
if(user)
|
||
|
res.json(user)
|
||
|
else
|
||
|
res.status(400).json({errors: [{param: 'password', msg: 'Invalid login'}]})
|
||
|
})
|
||
|
|
||
|
// TODO: Login link stuff
|
||
|
|
||
|
router.get('/', async (req, res) => {
|
||
|
// TODO: Get current user and session stuff
|
||
|
})
|
||
|
|
||
|
module.exports = router;
|