Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
Passport authentication via ajax

What I'm trying to accomplish is to do user authentication via ajax using passport. The recommended way of using passport is to trigger the authorisation process via a GET request which gets triggered using a regular <a> tag. After successful (or failed) authentication there is a redirect to a new page. Now instead of using a html link to trigger the route I want to do it via ajax. The problem here is that when I try this with a third party authentication strategy such as passport-facebook I get a CORS error:

XMLHttpRequest cannot load [the facebook auth URL]' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:8080' is therefore not allowed access.

Here is the relevant server side code:

// route for facebook authentication and login
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email'] }));

// handle the callback after facebook has authenticated the user
app.get('/auth/facebook/callback', (req, res, next) => {
  passport.authenticate('facebook', (err, user, info) => {
    req.login(user, function(err) {
      if(err) return next(err);
      return res.status(200).json({
        success: "All good man!"
      });
    })
  })(req, res, next);
});

While in the browser I simply do:

axios.get('/auth/facebook').then((response) => {
  console.log(response);
});

I found several similar questions but none of them seemed to have a satisfying answer. Is there a way to actually do this? Or a smart workaround? Maybe a way to still send back a JSON response even if the route was triggered using an html link?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I'm not sure this is possible. First, if Facebook doesn't enable CORS there's not much you can do about it (except use something like JSONP, but that also needs to be enabled). But more importantly, I think the way they do the login is not by giving you back a token that you could use, but to store a cookie. For that they need to be on their own domain and you can't fake that with Ajax. Lastly, I would imagine that it's part of their way of building trust with their users to always show the same login/authorisation form, so I would think that they would do everything they can to prevent you from going around this.

This is all hypothetical, mind, I haven't used any of the tools you mention :)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!