Internal OAuth Error: Failed to fetch user profile NodeJs with passport-google-oAuth2

482 Views Asked by At

Here is my code to authenticate user based on google email :

router.use(passport.initialize());

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

passport.deserializeUser((user, done) => {
    done(null, user);
});

passport.use(new GoogleStrategy({
    clientID: config.googleClientID,
    clientSecret: config.clientSecret,
    callbackURL: host + "/google-login/callback"
    }, (accessToken, refreshToken, profile, done) => {
        const user = {}
        user.name = profile.displayName;
        user.email = profile.emails[0].value;
        user.photo = profile.photos[0].value;
        return done(null, user);
    }
));

router.get('/', passport.authenticate('google', { scope: ['profile', 'email'] }));

router.get('/callback', passport.authenticate('google', { failureRedirect: '/signup' }),
    async (req, res) => {
        await register(req, res);
        res.redirect('/google-login/popup');
    });


router.get('/popup', (req, res, next) => {
    res.render('login/popup-close');
});

router.get('/popup-done', authorize(), async(req, res, next) => {

    const account = await getAccount({ id: req.user.id, include: ['firstImpression', 'role'] });
    // if(req.user.role === Role.Admin) return res.redirect(`/accounts/admin5852`);
    const { section, data } = req.query;

    if(section == 'checkout') {
        redirect();
    } else {
        if(account.firstImpression) {
            account.firstImpression = false;
            await account.save();
            res.redirect(`/accounts/dashboard#section=guide`);
        } else {
            redirect();
        }
    }

    function redirect() {
        if(section == 'undefined') {
            res.redirect(`/accounts/dashboard`);
        } else {
            res.redirect(data ? `/accounts/dashboard#section=${section}&data=${data}` : `/accounts/dashboard#section=${section}`);
        }
    }
    
});

I get this error sometimes Not all the times but sometimes and this makes it really hard to understand the bug:

{"name":"InternalOAuthError","message":"Failed to fetch user profile","oauthError":{"errno":-101,"code":"ENETUNREACH","syscall":"connect","address":"2a00:1450:4017:808::200a","port":443}}

How can I debug this? How to fix this?

0

There are 0 best solutions below