Nodejs React - Facebook Login - Cors issue

254 views Asked by At

I am trying to implement facebook login with passport and FacebookStrategy.

passport.use(
    new FacebookStrategy(
        {
            clientID: 'myclientid',
            clientSecret: 'myclientsecret',
            callbackURL: 'http://localhost:3333/api/auth/facebook/callback',
            profileFields: ['email', 'displayName', 'name'],
        },
        async function (accessToken, refreshToken, profile, done) {
            ...     
        },
    ),
)

Routes:

router.get('/facebook', passport.authenticate('facebook', { scope: 'email' }))
router.get('/facebook/callback', passport.authenticate('facebook'), AuthControllerV3.loginFacebookWeb)

But when calling the endpoint /facebook from the client, I get this error:

Access to XMLHttpRequest at 'https://www.facebook.com/v3.2/dialog/oauth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3333%2Fapi%2Fauth%2Ffacebook%2Fcallback&scope=email&client_id=myclientid' (redirected from 'http://localhost:3333/api/auth/facebook') 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.

I added in the client side this headers:

config.headers.common['Content-Type'] = 'application/json';
config.headers.common['Access-Control-Allow-Origin'] = true;

But still getting this cros error. What should I do. In facebook developers console I added in domain apps the domain http://localhost:3000 but nothing changed.

0

There are 0 answers