-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
41 lines (34 loc) · 1.13 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const Router = require('@koa/router')
const authenticate = require('./middlewares/authenticate')
const authorize = require('./middlewares/authorize')
const jwt = require('jsonwebtoken')
const { SECRET } = require('./config')
const USERS = require('./users')
const { saveMessageStats, getMessageStats } = require('./repositories/messageStats')
const router = new Router()
router.post('/login', (ctx) => {
const {username, password} = ctx.request.body
const user = USERS.find((user) => user.username === username && user.password === password)
if(!user) {
ctx.status = 401
// TODO(prod) better error handling mechanism + logging
ctx.body = { error: 'Username or password invalid' }
return
}
const token = jwt.sign({...user}, SECRET, { expiresIn: '15m' })
ctx.body = { token }
})
router.get('/logout', (ctx) => {
ctx.status = 200;
ctx.body = 'logged out'
})
router.post('/message', authenticate, (ctx) => {
saveMessageStats(ctx.request.body)
ctx.status = 200;
ctx.body = 'OK'
})
// TODO(prod) use constants for roles
router.get('/stats', authenticate, authorize('admin'), (ctx) => {
ctx.body = getMessageStats()
})
module.exports = router