-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (33 loc) · 903 Bytes
/
index.js
File metadata and controls
45 lines (33 loc) · 903 Bytes
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
import { App } from '@tinyhttp/app'
import { urlencoded } from 'milliparsec'
import getSession from 'next-session'
const app = new App()
app.use(urlencoded())
app.use(async (req, res, next) => {
req.session = await getSession({
secret: 'super secret text'
})(req, res)
next()
})
app.get('/', (_req, res) => {
res.send('Go to "/login" page to login')
})
app.post('/login', async (req, res) => {
const { body } = req
console.log(`Received body: ${JSON.stringify(req.body)}`)
if (body.user !== 'admin' || body.pwd !== 'admin') {
res.send('Incorrect login')
return
}
req.session.isLoggedIn = true
req.session.user = 'admin'
res.status(204).end()
})
app.get('/admin', async (req, res) => {
if (!req.session.isLoggedIn) {
res.status(403).send('Authorized personnel only!')
return
}
return res.send(`Welcome ${req.session.user}`)
})
app.listen(3000)