1
Relatively urgent help needed with Discord Login Integration on My Website
Hello,
I have a GitHub repository for a website I’m developing for Minecraft Civilization events that I host: https://nevm1nd.github.io/Nevmind-Civilization-Events/
Now, I tried setting up a a discord login system on the website to prevent impersonations, random applications, or submissions from people who are not in the Discord server for the events.
Before explaining what I did, it’s important to clarify that I have almost no experience with this topic. I know very little coding, and everything I’ve done so far was simply following instructions given to me by ChatGPT.
The first thing I was told to do was:
Create a folder on my computer named discord-login-backend.
Open CMD inside that folder and run the following commands:npm init -y npm install express axios dotenv
Create a file named index.js with the following code:
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 3000;
// Discord OAuth2 credentials from your Discord application
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REDIRECT_URI = process.env.REDIRECT_URI;
// Route for Discord login redirect
app.get('/auth/discord/callback', async (req, res) => {
const code = req.query.code;
if (!code) return res.send('No code provided');
try {
// Exchange code for access token
const tokenResponse = await axios.post('https://discord.com/api/oauth2/token', new URLSearchParams({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: 'authorization_code',
code: code,
redirect_uri: REDIRECT_URI,
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const accessToken = tokenResponse.data.access_token;
// Fetch user info
const userResponse = await axios.get('https://discord.com/api/users/@me', {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
// Optional: check if user is in your server
const guildId = process.env.GUILD_ID;
const memberResponse = await axios.get(`https://discord.com/api/users/@me/guilds`, {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
const isMember = memberResponse.data.some(g => g.id === guildId);
res.json({
user: userResponse.data,
inServer: isMember
});
} catch (err) {
console.error(err);
res.send('Error fetching Discord data');
}
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Create a .env file with the following:
CLIENT_ID=YOUR_DISCORD_CLIENT_ID
CLIENT_SECRET=YOUR_DISCORD_CLIENT_SECRET REDIRECT_URI=http://localhost:3000/auth/discord/callback
GUILD_ID=YOUR_DISCORD_SERVER_ID
I double-checked all the details, including REDIRECT_URI. I then went to the Discord application creation page, created an application, and entered the same redirect URI in the "Redirects" section as in the .env file.
After generating the authorization link and logging in via Discord, I received the error:
Error fetching Discord data
I suspected the issue might be because I was running everything locally, so I was advised to deploy it using Render.
Here’s what I did on Render:
I connected my GitHub repository to Render, named the service discord-login-backend.
Set the language to Node, branch to main.
Entered the same environment variables as in .env.
Set the build command to npm install and start command to node index.js.
I also updated the redirect URI to:
https://discord-login-backend.onrender.com/auth/discord/callback
Then I created a folder named discord-backend in my repository and put the local files inside (excluding .env and node_modules). The files I included were index.js, package.json, and package-lock.json. I also set this folder as the root directory in Render.
However, when I tried to use the new URL from the Discord OAuth2 application, I got the error:
Cannot GET /auth/discord/callback
Additionally, Render now shows the deployment status as "Failed" with the error:
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express-session' imported from /opt/render/project/src/discord-backend/index.js
This is relatively urgent because I am hosting an event soon and need the application system to work as soon as possible. Again, I have almost no experience with Node.js or backend coding, so any help would be greatly appreciated.
If you need more information from me, just ask, and I’ll provide it.
For immediate assistance, please email our customer support: [email protected]