-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (38 loc) · 1.01 KB
/
server.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
42
43
44
45
46
47
const restify = require('restify')
const uuid = require('uuid')
const server = restify.createServer({
name: 'Test API',
version: '1.0.0'
})
server.use(restify.plugins.acceptParser(server.acceptable))
server.use(restify.plugins.queryParser())
server.use(restify.plugins.bodyParser())
const sessions = {}
server.post({ name: 'login', path: '/login' }, function (req, res, next) {
console.log(`Logging in with ${req.body.username}`)
if (req.body.password !== '123') {
res.send(401)
return next()
}
const sessionId = uuid.v4()
sessions[sessionId] = {
username: req.body.username
}
res.send(200, {
sessionId: sessionId
})
return next()
})
server.get({ name: 'whoami', path: '/whoami' }, function (req, res, next) {
console.log('Whoami')
const sessionId = req.header('Authorization')
if (!sessionId || !sessions[sessionId]) {
res.send(400)
return next()
}
const session = sessions[sessionId]
res.send(200, {
username: session.username
})
})
module.exports = server