İlyax
İlyax

Reputation: 11

Why 0auth can not able to read user to whitelist them?

So basically I were creating a whitelist system in node.js to check if the user is whitelisted by checking the role. If user in guild and if the user has the role It will response as ACCEPTED but If he has no role then will response as DECLINED and I want you guys to add if the user is not in the GUILD then it will response as NONMEMBER if it has a error then response as ERROR. I don't even know why it is I've annoyed myself for like three hours and made me to ask you. so can you guys able to help me i hope you guys can thank you for reading and helping me!

const express = require('express');
const axios = require('axios');
const querystring = require('querystring');

const app = express();
const PORT = 3000;

const CLIENT_ID = '1332081657993367582'; 
const REQUIRED_ROLE_ID = "1325501603201028171"
const GUILD_ID = "1325501050471448647"
const CLIENT_SECRET = process.env.CLIENT_SECRET; 
const REDIRECT_URI = `https://dour-repeated-gasoline.glitch.me/validate`; 
const SCOPE = 'identify'; 

app.get('/login', (req, res) => {
    const discordOAuthURL = 
        `https://discord.com/oauth2/authorize?` +
        `client_id=${CLIENT_ID}` +
        `&response_type=token` +
        `&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` +
        `&scope=identify`;
    res.redirect(discordOAuthURL);
});

app.get('/validate', async (req, res) => {
    const { code } = req.query;

    if (!code) {
        return res.status(400).send('');
    }

    try {
        const tokenResponse = await axios.post('https://discord.com/api/oauth2/token', null, {
            params: {
                client_id: CLIENT_ID,
                client_secret: CLIENT_SECRET,
                grant_type: 'authorization_code',
                code,
                redirect_uri: REDIRECT_URI,
            },
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        });

        const accessToken = tokenResponse.data.access_token;

        const userGuilds = await axios.get('https://discord.com/api/v10/users/@me/guilds', {
            headers: { Authorization: "Bearer " + code },
        });

        const isRolePresent = userGuilds.data.some((guild) => guild.roles?.includes(REQUIRED_ROLE_ID));

        if (isRolePresent) {
            res.send("ACCEPTED");
        } else {
            res.send("DECLINED");
        }
    } catch (error) {
        console.error('Error during validation:', error.response?.data || error.message);
        res.status(500).send('Something went wrong try again later');
    }
});
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});```

Upvotes: 0

Views: 39

Answers (0)

Related Questions