-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (104 loc) · 3.86 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
/**
* Module dependencies.
*/
var express = require('express')
, engine = require('ejs-locals')
, home = require('./routes/home')
, map = require('./routes/map')
, http = require('http')
, path = require('path')
// , Geo = require('./models/geo')
, mongoskin = require('mongoskin');
passport = require('passport')
, util = require('util')
, GoogleStrategy = require('passport-google').Strategy,
fs = require('fs');
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new GoogleStrategy({
returnURL: 'http://maaap.herokuapp.com/auth/google/return',
realm: 'http://maaap.herokuapp.com/'
},
function(identifier, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's Google profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Google account with a user record in your database,
// and return that user instead.
profile.identifier = identifier;
return done(null, profile);
});
}
));
var app = express();
// all environments
app.configure(function() {
app.engine('ejs', engine);
app.set('port', process.env.PORT || 4000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
var db = require('mongoskin').db('mongodb://ciaran:[email protected]:57538/heroku_app20430910', {safe:true});
app.param('collectionName', function(req, res, next, collectionName) {
req.collection = db.collection(collectionName);
return next();
});
app.get('/', function(req, res) {
if(req.session.user){
//console.log("/ req",req.session.user);
req.user = req.session.user;
}
res.render('home/index',{ user: req.user });
});
app.get('/account', function(req, res){
res.render('home/account', { user: req.session.user });
});
// GET /auth/google
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Google authentication will involve redirecting
// the user to google.com. After authenticating, Google will redirect the
// user back to this application at /auth/google/return
app.get('/auth/google', passport.authenticate('google', {failureRedirect: '/login' }));
// GET /auth/google/return
// 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/google/return',
passport.authenticate('google', {failureRedirect: '/test2' }),
function(req, res) {
req.session.user = req.user;
console.log("auth/google/return",req.session.user);
res.redirect('/');
});
app.get('/logout', function(req, res){
delete req.session.user;
res.redirect('/');
});
app.listen(3000);
app.get('/collections/:collectionName', map.load);
app.post('/collections/:collectionName', map.create);
app.post('/collections/:collectionName/update', map.update);
app.post('/collections/:collectionName/delete', map.delete);
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});