Sunday, 9 April 2017

Page displays 'null' after authentication

I'm creating a simple PWA to draw in multiple data sources into one application. I'm currently trying to set up authentication using a combination of passport and the twitter-strategy.

After the user has successfully authenticated they're account, twitter redirects to the callback endpoint, with the valid user data.... essentially the auth has been successful. However, when sending the user back to the client side application a html document with the word null is presented, rather than the application.

With the following code, I expect:

  • Twitter to return to callback URL
  • Server to perform actions in authSuccess function
  • Redirect to the provided url on the client
  • routes.js to server the application shell via the catch all route
  • Client application to boot and handle the URL served

Currently, only the first two steps are successful, and the app simply displays null (with the correct url in the address bar), rather than the expected output. Changing the location of the writeHead() call to / works, and the user is authenticated as expected ().

routes.js

let users = require('../controllers/userController');

app.get('/user/auth/twitter/callback',
  passport.authenticate('twitter', {
    failWithError: true
  }),
  function(req, res) {
    console.log('[Twitter] Auth success route hit');
    users.authSuccess(req, res);
  },
  function(err, req, res, next) {
    console.log('[Twitter] Auth failure route hit');
    users.authFailure(err, req, res, next);
  }
);

app.get('*', function(req, res){
   console.log('[Route] Catch All: ' + req.path);
   res.sendFile(path.resolve(__dirname, '../../public/index.html'));
});

userController.js

authSuccess(req, res) {
  console.log('[User Controller] User',req.user);

  // Set some cookies here

  res.writeHead(302, {'Location': '/user/profile'});
  // res.redirect('/user/profile');
  res.end();
}

Any help much appreciated. If you need more code, just ask :)

Thanks



via tonyedwardspz

No comments:

Post a Comment