-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
56 lines (46 loc) · 1.26 KB
/
auth.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
"use strict";
var express = require('express');
var passport = require('passport');
var GitHubStrategy = require('passport-github').Strategy;
function message(msg) {
return [
'<!doctype html>',
'<script>',
'(function() {',
' window.opener.postMessage(' + JSON.stringify(msg) + ', "*");',
' setTimeout(window.close, 10);',
'})();',
'</script>'
].join('\n');
}
function createApp(opts) {
var app = express();
passport.serializeUser(function(user, done) {
done(null, JSON.stringify(user));
});
passport.deserializeUser(function(data, done) {
try {
var user = JSON.parse(data);
} catch (err) {
return done(err);
}
done(null, user);
});
passport.use(new GitHubStrategy({
clientID: opts.clientID,
clientSecret: opts.clientSecret,
callbackURL: opts.callbackURL,
scope: ['user', 'gist']
},function(accessToken, refreshToken, profile, done) {
done(null, {profile: profile, accessToken: accessToken});
}));
app.get('/github',
passport.authenticate('github'));
app.get('/github/callback',
passport.authenticate('github', {failureRedirect: '/'}),
function(req, res) {
res.send(message({user: req.user, type: 'auth'}));
});
return app;
}
module.exports = createApp;