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.

50 lines
975 B
JavaScript

const router = require('express-promise-router')()
const parseJSON = require('body-parser').json()
const db = require('../db')
const validate = require('./middleware/validators')
const loginValidation = [
validate.validEmail('email'),
validate.validPassword('password'),
validate.handleApiError
]
router.post('/', parseJSON, loginValidation, async (req, res) => {
const user = await db.user.login(
req.body.email,
req.body.password
)
5 years ago
if(!user){
return res.status(422).json({errors: [{
param: 'email',
msg: 'Invalid login'
},{
param: 'password',
msg: ' '
}]})
5 years ago
}
await db.session.create(req, user)
const {password_hash, ...result} = user
res.json(result)
})
// TODO: Login link stuff
router.get('/', async (req, res) => {
5 years ago
res.json(req.user)
})
router.get('/logout', async (req, res) => {
await db.session.end(req.session.uuid);
5 years ago
req.session = null
res.redirect('/')
})
module.exports = router;