bluegreymouse
bluegreymouse

Reputation: 137

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) {});

Upvotes: 3

Views: 1077

Answers (1)

hexacyanide
hexacyanide

Reputation: 91799

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);
});

Upvotes: 1

Related Questions