Discord Login Flow
Express.js serving static HTML files incorporating a Discord OAuth login flow to a protected dashboard.
Node.js Web Dashboards zlizz द्वारा
यह टेंपलेट डिप्लॉय करें
GitHub पर खोलें
परिचय
README.md
विषय-सूची
Discord Login Template
An Express template that adds a "Login with Discord" flow (OAuth2) and a protected dashboard page, using plain HTML/CSS/JS on the frontend — no templating engine required.
How it works
- User clicks Login with Discord on
index.html, which sends them to/auth/discord. - That route redirects them to Discord's consent screen.
- After approving, Discord redirects back to
/auth/discord/callbackwith a code. - The server exchanges that code for an access token, fetches the user's Discord profile, and stores it in a server-side session.
- The user is redirected to
/dashboard, which calls/api/userto fetch and display their info. /auth/logoutdestroys the session and sends them back to the login page.
/dashboard is protected — if there's no active session, requesting it redirects straight back to /. Static files are served without their .html extension (express.static is configured with extensions: ['html']), so /dashboard maps to public/dashboard.html and / maps to public/index.html.
Getting Started
Create a Discord application
- Go to the Discord Developer Portal and click New Application.
- Under OAuth2 → General, copy your Client ID and Client Secret.
- Under OAuth2 → Redirects, add:(update the port/domain if you change it later)
http://localhost:3000/auth/discord/callback
3. Set up your environment variables
Copy the example env file:
cp example.env .env
Then fill in .env:
PORT=3000
DISCORD_CLIENT_ID=your_client_id
DISCORD_CLIENT_SECRET=your_client_secret
DISCORD_REDIRECT_URI=http://localhost:3000/auth/discord/callback
SESSION_SECRET=any_long_random_string
Project Structure
.
├── index.js # Express server, session setup, OAuth routes
├── example.env # Rename to .env in production
├── package.json
├── example.env
└── public/
├── index.html # Login page
├── dashboard.html # Protected dashboard (client-side rendered)
└── css/
└── styles.css
Customizing
- Scopes — the login route requests
identify email. Add more scopes (e.g.guilds) in thescopeparameter inside the/auth/discordroute inindex.js, and update the callback to fetch/store any extra data you need. - Session storage — sessions currently live in memory, which resets on server restart and won't scale across multiple server instances. For production, swap in a persistent session store (e.g.
connect-redisorconnect-mongo). - Dashboard content —
dashboard.htmlcurrently shows username, tag, user ID, and email. Extend the/api/userroute inindex.jsto return more data, and update the script indashboard.htmlto display it. - Styling — all styles are in
public/css/styles.css.
Notes
- Requires Node 18+ (uses the built-in global
fetch). - Never commit your real
.envfile — onlyexample.envshould be checked into version control.
वेरिएबल
इन्हें आप डिप्लॉय करते समय भरते हैं। ये आपके डिप्लॉयमेंट के एनवायरनमेंट वेरिएबल बन जाते हैं।
PORTPORT Bot-hosting.net assigned Port ज़रूरीDISCORD_CLIENT_IDDISCORD_CLIENT_ID From Discord Developer Portal ज़रूरीDISCORD_CLIENT_SECRETDISCORD_CLIENT_SECRET From Discord Developer Portal ज़रूरी, सीक्रेटDISCORD_REDIRECT_URIDISCORD_REDIRECT_URI The callback link after the user approves the Discord Login flow ज़रूरीSESSION_SECRETSESSION_SECRET Randomly generated. See example.env ज़रूरी, सीक्रेट
इसे कैसे डिप्लॉय करें
- 1 डिप्लॉय पेज खोलेंअपने डिप्लॉयमेंट को नाम दें। पुष्टि करते समय आप साइन इन करते हैं।
- 2 इसके वेरिएबल भरेंटेंपलेट जो वैल्यू माँगता है, वे आपके डिप्लॉयमेंट के एनवायरनमेंट वेरिएबल के रूप में सेव होती हैं।
- 3 अपने रिसोर्सेज़ जाँचेंयह आपके प्लान के साझा पूल से लेता है, फिर लॉन्च होता है। हर अकाउंट फ़्री से शुरू होता है।