Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda