JavaScript How do you show that the user is authenticated after they authenticate with Facebook? show user mysql,show user linux,show user privileges mysql,

I'm following this tutorial, implementing Facebook authentication with Node and Passport.

Both the tutorial and the docs say to do this:

router.get('/auth/facebook/callback',
  passport.authenticate('facebook', {
    successRedirect: '/',
    failureRedirect: '/login'
  })
);

But then how does the user know whether or not they're authenticated? I'd like to send data to my front end that shows whether or not the user is logged in, and that I could use to update, say my navbar.

From what I understand, the process of authenticating with Facebook works like this:

  1. Send out GET /auth/facebook request.
  2. Hits route.
  3. 304 Redirect send back with url to the Facebook portal.
  4. Send out request to Facebook portal with callback url.
  5. User clicks "accept" on the portal.
  6. Facebook sends back a 304 Redirect with the url being your callback url.
  7. Request is sent out to your callback url, which in this case is GET /auth/facebook/callback.
  8. The cb of passport.use(obj, cb) gets run.
  9. A 304 Redirect is sent back, with the url being / or /login depending on success or failure.
  10. A request is sent out to / or /login.

I'm not sure how I could update my front end with the logged in user.

router.get('/auth/facebook/callback',
  passport.authenticate('facebook'),
  function(req, res) {
    var userCopy = JSON.parse(JSON.stringify(req.user));
    delete userCopy.auth;
    res.status(200).json(userCopy);
  }
);

This doesn't work because there isn't a $http.get().then() that receives the data.


I tried adding in some query parameters:

successRedirect: '/?foo=bar'

But it's not working for me. The url bar says http://localhost:3000/?foo=bar#_=_ and this doesn't log anything:

app.get('/*', function(req, res) {
  var url = path.resolve(__dirname + '/../client/' + envFolder + '/index.html');
  res.sendFile(url, null, function(err) {
    if (err) {
      return res.status(500).send(err);
    }
    console.log('here');
    console.log(req.query);
    return res.status(200).end();
  });
});

It works when I do successRedirect: '/home?facebook=true'. But inside of sendFile, I can't send anything else back. If I try res.send('Authenticated with Facebook') it gives me this error: Error: Can't set headers after they are sent.. Presumably because sendFile is sending back the file and setting some headers, and then I try to send something else back with different headers. So I'm not sure what to do.


The approach I arrived at is to use the run block:

function run($http, Session, $cookies) {
  $http
    .get('/current-user')
    .then(function(response) {
      Session.setUser(response.data);
      $cookies.put('userId', response.data._id);
    })
  ;
}
  1. I'm not sure that this is the best approach.
  2. It messes my tests up because they're not expecting the HTTP request and it sets the Session and cookies when I don't always want to do that.
Answer:1

I want to replace single new lines with one space like if I have string like this: "foo\nbar\nbaz\n\nquux" I want to have "foo bar baz\n\nquux" I've tried this regex: /\n(?!\n)/ but it don't work ...

I want to replace single new lines with one space like if I have string like this: "foo\nbar\nbaz\n\nquux" I want to have "foo bar baz\n\nquux" I've tried this regex: /\n(?!\n)/ but it don't work ...

I am trying to build a real time notifications module that functions in a similar manner to facebook's realtime notifications for example if a user requests friendship of another user a push ...

I am trying to build a real time notifications module that functions in a similar manner to facebook's realtime notifications for example if a user requests friendship of another user a push ...

I provide some additional services to websites that add my script to their site. Technically my script does not interact with actual functionality of the site, the most is does is read some ...

I provide some additional services to websites that add my script to their site. Technically my script does not interact with actual functionality of the site, the most is does is read some ...

I use jQuery & PDO to dynamic a pagination When I create some button which have id = xx Then use jQuery try get one of button id , It isn't work This is my code: $records = $...

I use jQuery & PDO to dynamic a pagination When I create some button which have id = xx Then use jQuery try get one of button id , It isn't work This is my code: $records = $...