After spending close to a week trying to implement passport strategy for google token unsuccessfully, I decided write it myself using the Nodejs client library for Google API. I'm passing the accesstoken from angularjs in a $http request and want to return the logged in user to the call after authenticating on the server side. I'm having trouble in passing the signed in user to the req object so I can pass it to the next middleware. Any help will be greatly appreciated.
**express code**
router
var gCtrl = require('./googleController.js');
app.post('/auth/google/accesstoken',
function (req, res) {
console.log('Im in the google auth middleware');
//below line is not returning the gUser so I can pass it to the next middleware
var gUser=gCtrl(req.token);
res.send(gUser);
},
function(req,res){
//do something with authenticated user
}
);
gCtrl Module that makes the Google API call
var ubCust = require('../models/ubCust.js');
var bodyParser = require('body-parser');
var google=require('googleapis');
var plus = google.plus('v1');
var OAuth2 = google.auth.OAuth2;
var oauth2client = new OAuth2('1094664898379-8u0muh9eme8nnvp95dafuc3rvigu4j9u.apps.googleusercontent.com','KQ_vNDaZzTvXDdsfgp5jqeZW','http://localhost:3000/auth/google/callback');
module.exports = function(acc_token){
//console.log(acc_token);
oauth2client.setCredentials({
access_token : acc_token
//refresh_token: token.refresh_token
});
plus.people.get({
userId:'me',
auth:oauth2client
},function(err,response){
if (err)
throw err;
//console.log('google user',response);
return response;
});
};