简体   繁体   中英

CORS error is only occurring on auth0 login request

I am making a simple fetch request but I keep getting a CORS error on my request for an Auth0 login.

fetch('http://localhost:3001/login')
.then((res) => res.json())
.then((data) => {console.log('success')})
.catch(error => {console.log(error)})} 

The server handles this request here:

app.get('/login', passport.authenticate('auth0', {
    prompt: 'login',
    scope: 'openid email profile',
    domain: process.env.AUTH0_DOMAIN,
    clientID: process.env.AUTH0_CLIENT_ID,
    clientSecret: process.env.AUTH0_CLIENT_SECRET,
    callbackURL: process.env.AUTH0_CALLBACK_URL || 'http://localhost:3000/callback'

}), (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
    console.log('hit')
    res.redirect('/');
});

I have been playing around with formatting for a while now. I just don't see what is causing the error.

So it looks like instead of making a GET request I needed to actually route my user to the desired url. I fixed my issue by omitting the button onClick and wrapping things in an <a>

<a href="http://localhost:3001/login">
  <button 
    id="loginButton">
      Log In
  </button>
</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM