-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (59 loc) · 1.75 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
require.paths.unshift('.')
/* Load Github keys */
try {
var keys = require('keys')
} catch(e) {
console.log("Could not load keys.js");
}
/**
* Module dependencies.
*/
var express = require('express'),
auth = require('connect-auth');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.use(express.cookieParser());
app.use(express.session({ secret: 'butts' }));
app.use(
auth([
auth.Github({
appId : keys.id
, appSecret: keys.secret
, callback: keys.callbackAddress
, scope: "user,repo,gist"})
])
);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.send('Hello, Internet. Would you like to <a href="/github">log in with github</a>?');
});
app.get('/github', protect, function(req, res){
res.send('<p>Hello! Here is some JSON that proves you are logged in:<p>' +
'<p>' + JSON.stringify( req.getAuthDetails() ) + '</p>');
});
var port = process.env.PORT || 4000;
app.listen(port);
function protect(req, res, next) {
if( req.isAuthenticated() ) next();
else {
req.authenticate(["github"], function(error, authenticated) {
if( error ) next(new Error("Problem authenticating"));
else {
if( authenticated === true)next();
else if( authenticated === false ) next(new Error("Access Denied!"));
else {
// Abort processing, browser interaction was required (and has happened/is happening)
}
}
})
}
}
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);