Discord Login

I have a discord login with Express.js
app.get("/discord", async (req, res) => {
const code = req.query.code;
const params = new URLSearchParams();
params.append("client_id", process.env.LOGIN_CLIENT_ID);
params.append("client_secret", process.env.LOGIN_CLIENT_SECRET);
params.append("grant_type", "authorization_code");
params.append("code", code); // Ensure code is correctly being passed here
params.append("redirect_uri", "http://localhost:3001/discord");

console.log('Client ID:', process.env.LOGIN_CLIENT_ID);
console.log('Client Secret:', process.env.LOGIN_CLIENT_SECRET);
console.log('Code:', code);

try {
const response = await axios.post('https://discord.com/api/oauth2/token', params);
const { access_token, token_type } = response.data;
const userDataResponse = await axios.get("https://discord.com/api/users/@me", {
headers: {
Authorization: `${token_type} ${access_token}`
}
});
console.log(`Data: ${userDataResponse.data.access_token}`);

const user = {
discordid: userDataResponse.data.id,
name: userDataResponse.data.username,
discord: true,
acces_token: response.data.access_token
};
let guildid;
console.log(`username: ${userDataResponse.data.username}`);
console.log(`Email: ${userDataResponse.data.email}`);
if (userDataResponse.data.username === "julianrjc3") {
guildid = "1233925574070767696";
} else {
guildid = "1230258666146365481";
}
req.session.user = user;
req.session.guildid = guildid;
return res.redirect("/dashboard");
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
});
app.get("/discord", async (req, res) => {
const code = req.query.code;
const params = new URLSearchParams();
params.append("client_id", process.env.LOGIN_CLIENT_ID);
params.append("client_secret", process.env.LOGIN_CLIENT_SECRET);
params.append("grant_type", "authorization_code");
params.append("code", code); // Ensure code is correctly being passed here
params.append("redirect_uri", "http://localhost:3001/discord");

console.log('Client ID:', process.env.LOGIN_CLIENT_ID);
console.log('Client Secret:', process.env.LOGIN_CLIENT_SECRET);
console.log('Code:', code);

try {
const response = await axios.post('https://discord.com/api/oauth2/token', params);
const { access_token, token_type } = response.data;
const userDataResponse = await axios.get("https://discord.com/api/users/@me", {
headers: {
Authorization: `${token_type} ${access_token}`
}
});
console.log(`Data: ${userDataResponse.data.access_token}`);

const user = {
discordid: userDataResponse.data.id,
name: userDataResponse.data.username,
discord: true,
acces_token: response.data.access_token
};
let guildid;
console.log(`username: ${userDataResponse.data.username}`);
console.log(`Email: ${userDataResponse.data.email}`);
if (userDataResponse.data.username === "julianrjc3") {
guildid = "1233925574070767696";
} else {
guildid = "1230258666146365481";
}
req.session.user = user;
req.session.guildid = guildid;
return res.redirect("/dashboard");
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
});
But if i want to get the servers where the bot and the user is in and where the user can manage the guild: I get this error
2 Replies
d.js toolkit
d.js toolkit6mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
JulianRJC
JulianRJCOP6mo ago
app.get("/servers", async (req, res) => {
if (isLoggedIn === false) {
return res.redirect('/');
}
const userGuildResponse = await axios.get("https://discord.com/api/users/@me/guilds", {
headers: {
Authorization: `Bearer ${req.session.user.access_token}`
}
})

const userGuilds = userGuildResponse.data.filter(guild => {
return (guild.permissions & 0x20) === 0x20; // Check "MANAGE_GUILD" permission
});

res.render('auth/servers', { guilds: userGuilds });

})
app.get("/servers", async (req, res) => {
if (isLoggedIn === false) {
return res.redirect('/');
}
const userGuildResponse = await axios.get("https://discord.com/api/users/@me/guilds", {
headers: {
Authorization: `Bearer ${req.session.user.access_token}`
}
})

const userGuilds = userGuildResponse.data.filter(guild => {
return (guild.permissions & 0x20) === 0x20; // Check "MANAGE_GUILD" permission
});

res.render('auth/servers', { guilds: userGuilds });

})
data: { message: '401: Unauthorized', code: 0 } I have the scopes for "identify" And "Guilds"
Want results from more Discord servers?
Add your server