6

I'm using Passport.js to authenticate with Google via OAuth (I'm using the passport-google-oauth strategy). It works fine, but I'm currently redirecting the user to "/", and I'd like to send them to "/" plus the current hash tag. I can send the hash value in a query string parameter, but I can't seem to set that value to the callbackURL property of the object that I'm passing to authenticate.

Can someone provide an example or explanation as to the correct way to do this? I'm not beholden to using the query string, it just seemed the most straight-forward route, but I'm open to using a session variable or something else, if that would be easier or better practice.

Thank you.

laggingreflex
  • 26,275
  • 28
  • 123
  • 172
Mike Pateras
  • 14,127
  • 29
  • 93
  • 131
  • If by "hash tag" you mean the fragment portion of a URL, be aware that browsers never send that information when making requests, so the only way to know it is if you set it yourself. – ebohlman Sep 06 '12 at 06:19
  • @ebohlman That's simply untrue. – Beau Dec 19 '12 at 20:47

1 Answers1

3

You can achieve this effect by storing the return url in the session.

// server
var app, express;

express = require('express');

app = express();

app.configure(function() {
  app.use(express.cookieSession({secret: 'shh'}));
});

app.get('/auth/twitter', function(req, res, next) {
  // to return to '/#/returnHash', request this url:
  // http://example.com/auth/twitter?return_url=%2F%23%2FreturnHash

  // on the client you can get the hash value like this:
  // encodeURIComponent("/"+window.location.hash)
  req.session.return_url = req.query.return_url;
  next();
}, passport.authenticate('twitter'));

app.get('/auth/twitter/callback', passport.authenticate('twitter', {
  failureRedirect: '/login'
}), function(req, res) {
  var url = req.session.return_url;
  delete req.session.return_url;

  // redirects to /#/returnHash
  res.redirect(url);
});
nrw
  • 819
  • 2
  • 7
  • 22