0

I'm trying to implement google+ authentication using Google Passport Strategy and return the access token to client side(Angular) for subsequent requests. But I keep getting the error at client side. "XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur…=1094664898379-8u0muh9eme8nnvp95dafuc3rvigu4j9u.apps.googleusercontent.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access." that I get while implementing google strategy for authentication in my web app. I use Angular framework at client side and Express on server.

I've also referred to following previous posts for solution but to no avail. The solutions offered are to use a simple href at Client Side, but I dont know how the access token can be passed to client side for making subsequent API requests. Any help will be greatly appreciated.

Angular/Node/Express/Passport - Issues when connecting to facebook(CORS) Angular/Node/Express/Passport Cross Domain Problems - Enable CORS Passport Facebook Authentication

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

After a lot of searching which was frustrating, I finally was able to workaround this problem by using google token strategy for authentication. Here I authenticate the user at the client side first and then authenticate the token at the server end. The code snippet below is for fb but same applies for google as well.

app.post('/auth/facebook/accesstoken',
      passport.authenticate('facebook-token',{session: false}),
      function (req, res) {
        // do something with req.user
        if(req.user){
            console.log('fb user authenticated');
            res.send(req.user);
        }
        else{
            console.log('no entry');
            res.send(401);
        }
      }
    );
  • Mine is SPA with templating being driven by Angular. If this had not worked I'd have ended up using href to call the passport strategy for google oath and struggled to pass the authenticated user back to client side. Or I wouldve ended up using server template engine like EJS for this purpose making my architecture complicated – Rajkumar Kaliyaperumal Nov 02 '16 at 08:01