-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
53 lines (40 loc) · 1.31 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
48
49
50
51
52
53
const env = require('dotenv').config()
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const passport = require('passport')
const { ExtractJwt, Strategy: JwtStrategy } = require('passport-jwt')
if (env.error) throw env.error
const { pickOnly } = require('./utils/')
const { seedAdmin, levelDB } = require('./utils/db')
const usersdb = levelDB('users')
const app = express()
const jwtOptions = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET
}
passport.use(
new JwtStrategy(jwtOptions, async (payload, cb) => {
try {
if (!payload.userName) throw new Error('User not found!')
const user = await usersdb.get(payload.userName)
cb(null, pickOnly(['userName', 'email', 'admin'], user))
} catch (err) {
cb(null, false)
}
})
)
app.use(cors())
app.use(bodyParser.json())
app.use(passport.initialize())
const authenticate = passport.authenticate('jwt', { session: false })
app.use('/auth', require('./routes/auth')(usersdb))
app.use('/meals', require('./routes/meals'))
app.get('/admin', authenticate, (req, res) => {
res.json({ admin: req.user.admin })
})
app.listen(5000, function () {
seedAdmin(usersdb).then(() =>
console.log('Server is running on http://localhost:5000/')
)
})