2013-05-06 03:38:29 -04:00
|
|
|
module.exports = function(app, passport) {
|
2013-09-04 03:05:47 -04:00
|
|
|
app.get('/auth', function(req, res, next) {
|
|
|
|
console.dir(req.headers);
|
|
|
|
req.session.redirectUrl = req.headers['referer'];
|
|
|
|
|
|
|
|
passport.authenticate('google', {
|
2013-05-06 03:38:29 -04:00
|
|
|
accessType: 'offline',
|
|
|
|
scope: [
|
|
|
|
'https://www.googleapis.com/auth/userinfo.profile',
|
|
|
|
'https://www.googleapis.com/auth/userinfo.email',
|
|
|
|
'https://www.googleapis.com/auth/calendar'
|
2013-09-04 03:05:47 -04:00
|
|
|
]})(req, res, next);
|
|
|
|
});
|
2013-05-06 03:38:29 -04:00
|
|
|
|
|
|
|
app.get('/auth/callback', function(req, res, next) {
|
2013-07-19 02:38:07 -04:00
|
|
|
var options = {
|
|
|
|
callbackURL: 'http://' + req.headers['x-forwarded-host'] + '/auth/callback'
|
|
|
|
};
|
|
|
|
console.log(options);
|
|
|
|
passport.authenticate('google', options, function(err, user, info) {
|
2013-06-26 03:54:22 -07:00
|
|
|
var redirectUrl = '/';
|
|
|
|
|
2013-05-06 03:38:29 -04:00
|
|
|
if (err) { return next(err); }
|
|
|
|
if (!user) { return res.redirect('/login/error'); }
|
|
|
|
|
2013-06-26 03:54:22 -07:00
|
|
|
if (req.session.redirectUrl) {
|
|
|
|
redirectUrl = req.session.redirectUrl;
|
|
|
|
req.session.redirectUrl = null;
|
|
|
|
}
|
|
|
|
|
2013-09-04 03:05:47 -04:00
|
|
|
if (redirectUrl.indexOf('/login') != -1) {
|
|
|
|
redirectUrl = '/';
|
|
|
|
}
|
|
|
|
|
2013-05-06 03:38:29 -04:00
|
|
|
req.logIn(user, function(err) {
|
|
|
|
if (err) { return next(err); }
|
|
|
|
});
|
2013-06-26 03:54:22 -07:00
|
|
|
|
|
|
|
res.redirect(redirectUrl);
|
2013-05-06 03:38:29 -04:00
|
|
|
})(req, res, next);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
requiresUiLogin: function(req, res, next) {
|
|
|
|
if (!req.isAuthenticated()) {
|
2013-09-04 03:05:47 -04:00
|
|
|
// req.session.redirectUrl = req.url;
|
2013-05-06 03:38:29 -04:00
|
|
|
return res.redirect('/login');
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
requiresApiAccess: function(req, res, next) {
|
|
|
|
if (!req.isAuthenticated()) {
|
|
|
|
return res.send(403);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
2013-07-19 02:38:07 -04:00
|
|
|
};
|