-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (59 loc) · 1.8 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require('express');
const bodyParser = require('body-parser');
const mongodb = require('./data/database');
const dotenv = require('dotenv').config()
const session = require('express-session');
const passport = require('passport');
const GitHubStrategy = require('passport-github2').Strategy;
const cors = require('cors');
const port = process.env.PORT || 3000;
const app = express();
app
.use(bodyParser.json())
.use(session({
secret: "secret",
resave: false,
saveUninitialized: true,
//cookie: { secure: true }
}))
.use(passport.initialize())
.use(passport.session())
.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods',
'POST, GET, PUT, PATCH, DELETE, OPTIONS'
);
next();
})
.use(cors({ methods: ['GET', 'POST', 'DELETE', 'UPDATE', 'PUT', 'PATCH']}))
.use(cors({ origin: '*' }))
.use("/", require("./routes/index.js"));
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL
},
function(accessToken, refreshToken, profile, done) {
return done(null, profile);
}
));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
app.get('/github/callback', passport.authenticate('github', {
failureRedirect: '/api-docs', session:false}),
(req, res) => {
req.session.user = req.user;
res.redirect('/');
});
mongodb.initDb((err) => {
if(err) {
console.log(err);
}
else {
app.listen(port, () =>{console.log(`Database is listening and node Running on port ${port}`)});
}
});