forked from JEckfeldt/InkedIn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
39 lines (31 loc) · 1.08 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
require('dotenv').config()
const express = require('express')
const { join } = require('path')
const passport = require('passport')
const { User, Post, Comment } = require('./models')
const { Strategy: JWTStrategy, ExtractJwt } = require('passport-jwt')
const app = express()
app.use(express.static(join(__dirname, 'public')))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(passport.initialize())
app.use(passport.session())
passport.use(User.createStrategy())
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser((id, done) => {
User.findOne({ id })
.then(user => done(null, user))
.catch(err => done(err, null))
})
passport.use(new JWTStrategy({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.SECRET
}, ({ id }, cb) => User.findOne({ where: { id }, include: [Post, Comment]})
.then(user => cb(null, user))
.catch(err => cb(err))))
app.use(require('./routes'))
require('./db').sync()
.then(() => app.listen(process.env.PORT || 3000))
.catch(err => console.log(err))