-1

I am trying to put a button on my website which will let the user authorize access to his YouTube account. I am using Passport for the authentication. My code is using fetch to send the request. When I click on the button, I get:

Access to fetch at 'https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fgooglecb&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&client_id=...' (redirected from 'http://localhost:3000/google') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

If I use an href to /google it works.

app.js:

const appConfig = require('./config.js');
const cors = require('cors')
const express = require('express');
const passport = require('passport');
const path = require('path');
const YoutubeV3Strategy = require('passport-youtube-v3').Strategy;

const index = require('./routes/index');

const app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(cors());

app.use(passport.initialize());
app.use(passport.session());

passport.use(new YoutubeV3Strategy({
  clientID: appConfig.youtube.id,
  clientSecret: appConfig.youtube.secret,
  callbackURL: 'http://localhost:3000/googlecb',
  scope: ['https://www.googleapis.com/auth/youtube'],
},
function (accessToken, refreshToken, profile, cb) {
  const user = {
    accessToken: accessToken,
    refreshToken: refreshToken,
    profile: profile,
  };
  return cb(null, user);
}));

passport.serializeUser((user, cb) => {
  cb(null, user);
});

passport.deserializeUser((obj, cb) => {
  cb(null, obj);
});

app.use('/', index);

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.use(function(err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

index.js:

const express = require('express');
const passport = require('passport');

const router = express.Router();

router.get('/', function(req, res, next) {
  res.render('index');
});

router.get('/google', async (req, res) => {
  passport.authenticate('youtube')(req, res, () => res.send('ok'));
});

router.get('/googlecb', passport.authenticate('youtube'), async (req, res) => {
  const name = req.user.profile.displayName;
  console.log('googlecb name: ' + name);
  return res.send(req.user);
});

module.exports = router;

index.ejs:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p><button id="auth" onclick="go()">Authorize</button><p>
    <p><a href="/google">Authorize</a><p>
    <script> 
async function go() {
  console.log('go');
  await fetch('/google',
    {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    },
  )
  .then(response => {
    console.log('response');
    return response.json();
  })
  .then(data => {
    console.log('data');
  });
}
    </script>
  </body>
</html>
Adam
  • 73
  • 8
  • In the error message `Access to fetch at 'https:...' (redirected from 'http://localhost:3000/google')`, what’s the URL that you elided? – sideshowbarker Sep 08 '20 at 05:56
  • 1
    Whatever the URL is that you elided, the server at the URL needs to be CORS-enabled. If you’re redirecting a frontend request to that URL, then just CORS-enabling your own `http://localhost:3000/` server isn’t sufficient. Instead the server at the elided URL also needs to be CORS-enabled. And you can’t work around that by using your own `http://localhost:3000/` server in between in the way you are now — because what you have set up now is causing your frontend JavaScript code to receive the response directly from the server at the elided URL, not from your `http://localhost:3000/` server. – sideshowbarker Sep 08 '20 at 05:59
  • possible duplicates: https://stackoverflow.com/questions/48177354/vue-axios-passport-js-google-results-in-cors-access-control-allow-origin https://stackoverflow.com/questions/49814440/access-control-allow-origin-header-problems-with-login https://stackoverflow.com/questions/45487449/no-access-control-allow-origin-header-is-present-on-the-requested-resource https://stackoverflow.com/questions/50317575/passportjsangular5-no-access-control-allow-origin-header-is-present-on-the – Adam Sep 09 '20 at 00:01
  • possible duplicates: https://stackoverflow.com/questions/63799912/i-am-trying-to-connect-ssos-to-the-frontend-angular-but-i-always-get-cors-err https://stackoverflow.com/questions/63799074/cors-error-when-sending-request-from-react-frontend-to-google-oauth-passportjs-o – Adam Sep 09 '20 at 00:05

2 Answers2

1

OK this is an open issue with Passport:

https://github.com/jaredhanson/passport/issues/582#issuecomment-506283986

I should use the href and not fetch.

Possible duplicate of

Cors error when sending request from React frontend to Google Oauth PassportJS on backend

Adam
  • 73
  • 8
0

Solution is to use an anchor tag with the url of your backend's route for this. Like if you have this.router.get("/auth/google", passport.authenticate("google", { scope: ["email", "profile"], successRedirect: "http://localhost:3000" })); then put an anchor tag for <a href="localhost:3000/auth/google>Click me</a>

plutownium
  • 1,220
  • 2
  • 9
  • 23