-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (57 loc) · 1.67 KB
/
index.js
File metadata and controls
76 lines (57 loc) · 1.67 KB
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
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import passport from 'passport';
import path from 'path';
import { Strategy } from 'passport-github2';
let session = require('express-session');
/** passport setup */
passport.use(new Strategy({
clientID: '4fb763621db9d2a88652',
clientSecret: '16e6728d73ca1f81cbaad89f964606fa1cf91e56',
callbackURL: "http://localhost:3000/auth/github/callback"
},
function(accessToken, refreshToken, user, cb) {
return cb(null,user);
}
));
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
const app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
/** set app */
app.use(cors());
app.use(bodyParser.json({
limit: '50mb'
}));
//passport middleware
app.use(session({
secret: 's3cr3t',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public'));
const isAuthenticated = async (req, res, next) => {
if (req.isAuthenticated()) { return next(); }
res.redirect('/');
}
app.get('/', (req, res) => {
res.render('index');
});
app.get('/account', isAuthenticated, (req, res) => {
res.render('success', { 'user' : req.user._json});
});
app.get('/auth/github', passport.authenticate('github', { scope: [ 'user:email' ] }));
app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/account', failureRedirect: '/' }));
app.use('/auth/logout', (req, res) => {
req.logout();
res.redirect('/');
});
app.listen(3000);