2018-03-16 16:12:06 +00:00
|
|
|
import express from 'express'
|
2017-07-08 16:04:47 +00:00
|
|
|
|
2017-07-08 16:09:47 +00:00
|
|
|
// Create express router
|
|
|
|
const router = express.Router()
|
2017-07-08 16:04:47 +00:00
|
|
|
|
2017-07-08 16:09:47 +00:00
|
|
|
// Transform req & res to have the same API as express
|
|
|
|
// So we can use res.status() & res.json()
|
|
|
|
var app = express()
|
|
|
|
router.use((req, res, next) => {
|
|
|
|
Object.setPrototypeOf(req, app.request)
|
|
|
|
Object.setPrototypeOf(res, app.response)
|
|
|
|
req.res = res
|
|
|
|
res.req = req
|
|
|
|
next()
|
|
|
|
})
|
2017-07-08 16:04:47 +00:00
|
|
|
|
2017-07-08 16:09:47 +00:00
|
|
|
// Add POST - /api/login
|
|
|
|
router.post('/login', (req, res) => {
|
|
|
|
if (req.body.username === 'demo' && req.body.password === 'demo') {
|
|
|
|
req.session.authUser = { username: 'demo' }
|
|
|
|
return res.json({ username: 'demo' })
|
|
|
|
}
|
|
|
|
res.status(401).json({ message: 'Bad credentials' })
|
|
|
|
})
|
2017-07-08 16:04:47 +00:00
|
|
|
|
2017-07-08 16:09:47 +00:00
|
|
|
// Add POST - /api/logout
|
|
|
|
router.post('/logout', (req, res) => {
|
|
|
|
delete req.session.authUser
|
|
|
|
res.json({ ok: true })
|
|
|
|
})
|
2017-07-08 16:04:47 +00:00
|
|
|
|
2017-07-08 16:09:47 +00:00
|
|
|
// Export the server middleware
|
2018-03-16 16:12:06 +00:00
|
|
|
export default {
|
2017-07-08 16:09:47 +00:00
|
|
|
path: '/api',
|
|
|
|
handler: router
|
2017-10-31 13:43:55 +00:00
|
|
|
}
|