-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
81 lines (68 loc) · 1.77 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const express = require('express');
const app = express();
const mongoose = require("mongoose");
const path = require("path");
require('dotenv').config()
const {
ExtractJwt,
Strategy
} = require('passport-jwt');
const passport = require('passport');
const cors = require('cors');
const bodyParser = require('body-parser');
const config = require("./config");
const User = require("./models/user");
const apis = require("./apis");
// MongoDB connection
mongoose.connect(config.db, {
useNewUrlParser: true,
}).then(success => {
console.log("MongoDB connected!!!", config.db);
}).catch(err => {
console.log("MongoDB connection failed!!!", err)
});
app.use(cors({
exposedHeaders: config.corsHeaders
}));
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json({
limit: "1000kb"
}));
app.use(passport.initialize({
session: false
}))
const jwtOptions = {
secretOrKey: config.jwtSecret,
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
passReqToCallback: true
}
passport.serializeUser(function (user, done) {
console.log(user)
done(null, user.id);
})
passport.deserializeUser(async function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
})
passport.use('jwt', new Strategy(jwtOptions, async (req, jwt_payload, done) => {
console.log(jwt_payload);
try {
User.findOne({ email: jwt_payload.id }, function (err, user) {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
}
});
} catch (ex) {
return done(null, false);
}
}));
apis(app);
app.listen(process.env.PORT || 9090);