-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
159 lines (138 loc) · 5.33 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var express = require('express')
, passport = require('passport')
, util = require('util')
, GitHubStrategy = require('passport-github').Strategy
, ld = require('linked-data-creator-api')
, dbUrl = process.env.MONGOLAB_URI || 'mongodb://localhost/test2';
var GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID || "e71c9f79ab961a823bcc"
var GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET || "14fe5897e4cb75ddcb0a707e3331e7fdf08bcb10";
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete GitHub profile is serialized
// and deserialized.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Use the GitHubStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and GitHub
// profile), and invoke a callback with a user object.
passport.use(new GitHubStrategy({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
scope: ['repo'],
callbackURL: process.env.GITHUB_CALLBACK || 'http://localhost:3000/auth/github/callback'
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
profile.accessToken = accessToken;
// To keep the example simple, the user's GitHub profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the GitHub account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}
));
var app = express();
// configure Express
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.get('/', getSelf, function(req, res){
res.render('index', { user: req.user, profile: req.profile });
});
app.post('/create_profile', ensureAuthenticated, createProfile);
app.get('/login', function(req, res){
res.render('login', { user: req.user });
});
// GET /auth/github
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in GitHub authentication will involve redirecting
// the user to github.com. After authorization, GitHubwill redirect the user
// back to this application at /auth/github/callback
app.get('/auth/github',
passport.authenticate('github'),
function(req, res){
// The request will be redirected to GitHub for authentication, so this
// function will not be called.
});
// GET /auth/github/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/login' }),
function(req, res) {
ld.configure({
token: req.user.accessToken,
username: req.user.username
});
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
ld.connect(dbUrl, function(err) {
if (err) return console.log(err);
// for cloud9 app.listen(process.env.PORT, process.env.IP);
app.listen(process.env.PORT || 3000);
console.log('server started');
});
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}
function getSelf(req, res, next) {
var url;
if (req.isAuthenticated()) {
url = 'https://raw.githubusercontent.com/' + req.user.username +'/open-data-self/master/data.json';
ld.get(url, function (err, entity) {
if (err && err.code !== 404) console.log(err);
else if (err && err.code === 404) {} // do nothing
else req.profile = entity;
next();
});
}
else next();
}
function createProfile(req, res) {
ld.create({
isSelf: true,
attrs: {
type: 'Person',
url: req.body.url,
displayName: req.body.displayName,
image: req.body.image
}
}, function (err, entity) {
if (err) console.log(err);
res.redirect('/');
});
}