Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner node.js app using passport + googleapis

I'm working with Node.js to try and make a book library app using the Google books API and passport with the Google strategy for authentication. So far so good I can authenticate and access the API. Now maybe a silly question, how to get the data back to use on my collection view? I have access to the Google data after the authentication and then I need to redirect to the collection but how do I build that data into my new view?

app.get('/auth/google', passport.authenticate('google', {
  scope: ['https://www.googleapis.com/auth/books', 'https://www.googleapis.com/auth/userinfo.profile']
}));

app.get('/auth/google/callback', passport.authenticate('google', {failureRedirect: '/'}), function (req, res) {
  // Successful authentication, get data and redirect to user collection
  googleapis.discover('books', 'v1').execute(function (err, client) {
    oath2Client.credentials = {
      access_token: req.user.accessToken,
      refresh_token: req.user.refreshToken
    };
    var req1 = client.books.mylibrary.bookshelves.list().withAuthClient(oath2Client);
    req1.execute(function (err, bookshelves) {});
  });
  res.redirect('/collection');
});

app.get('/collection', routes.collection, function (req, res) {});
like image 645
bluegreymouse Avatar asked Mar 24 '26 04:03

bluegreymouse


1 Answers

What you could do is you could store the data in a session variable, and then fetch it from another route. Here's an example with storing a string and accessing it from another page:

//enable session support
app.use(express.cookieParser());
app.use(express.session({
  secret: 'secret key for signed cookies'
}));

app.get('/foo', function(req, res) {
  req.session.property = 'property value';
  res.redirect('/bar');
});

app.get('/bar', function(req, res) {
  //the session variables can be accessed here too
  res.send(req.session.property);
});
like image 131
hexacyanide Avatar answered Mar 26 '26 18:03

hexacyanide