nicholastay / passport-discord

Passport strategy for authentication with Discord (discordapp.com)
ISC License
172 stars 55 forks source link

InternalOAuthError: Failed to fetch user's guilds #29

Closed nobu-sh closed 3 years ago

nobu-sh commented 4 years ago

When discord authorize screen shows up, if authorize is clicked more than once it redirects to a ?code= route and states this InternalOAuthError: Failed to fetch user's guilds at C:\Users\Nobu\Desktop\website\src\node_modules\passport-discord\lib\strategy.js:108:32 at passBackControl (C:\Users\Nobu\Desktop\website\src\node_modules\oauth\lib\oauth2.js:132:9) at IncomingMessage.<anonymous> (C:\Users\Nobu\Desktop\website\src\node_modules\oauth\lib\oauth2.js:157:7) at IncomingMessage.emit (events.js:327:22) at endReadableNT (_stream_readable.js:1201:12) at processTicksAndRejections (internal/process/task_queues.js:84:21)

Is there a way I can fix this or is this an internal issue?

my code is

**discordstrategy.js**

passport.serializeUser((user, done) => {
    done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
    const user = await DiscordUser.findById(id);
    if (user) {
        done(null, user);
    }
});
passport.use(new DiscordStrategy({
    clientID: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET,
    callbackURL: process.env.CLIENT_REDIRECT,
    scope: ['identify', 'email', 'guilds']
}, async (accessToken, refreshToken, profile, done) => {
    try {
        const user = await DiscordUser.findOne({ discordId: profile.id });
        if (user) {
            user.username = profile.username;
            user.email = profile.email;
            await user
            .save();
            done(null, user);
        } else {
            log.general(`Adding ${profile.username} to database`);
            const newUser = await DiscordUser.create({
                discordId: profile.id,
                username: profile.username,
                email: profile.email
            });
            const saveUser = await newUser.save();
            done(null, saveUser);
        }
    } catch(err) {
        log.error(err);
        done(err, null);
    }
}));

**auth.js**

router.get('/', passport.authenticate('discord'))
router.get('/redirect', passport.authenticate('discord', {
    failureRedirect: '/500',
    successRedirect: '/dashboard'
}));
router.get('/logout', (req, res) => {
    if (req.user) {
        req.logout();
        res.redirect('/');
    } else {
        res.redirect('/');
    }
});
Xzandro commented 4 years ago

Can confirm. Pretty sure the user actually gets rate limited, since you can only request the user guilds endpoint once a second.

tonestrike commented 3 years ago

I am not 100% certain this is related, but I ran into issues because this callback is not returned: https://github.com/nicholastay/passport-discord/blob/master/lib/strategy.js#L92

If it fails to get the access token for some reason, it still continues on and would therefore cause issues getting the guilds as well.

I have fixed this and a couple other issues in my fork of the package here: https://github.com/tonestrike/passport-discord

nobu-sh commented 3 years ago

I am not 100% certain this is related, but I ran into issues because this callback is not returned: https://github.com/nicholastay/passport-discord/blob/master/lib/strategy.js#L92

If it fails to get the access token for some reason, it still continues on and would therefore cause issues getting the guilds as well.

I have fixed this and a couple of other issues in my fork of the package here: https://github.com/tonestrike/passport-discord

Okay, Thanks! I long overcame the issue by just making my own discord oauth lib, will check out though