How should I safely add Discord login to my website?

0
2
Asked By MapleOrbit27 On

I'm in high school and building my first website, including a login system. I'd like users to sign in with their Discord account, but I'm confused about which credentials are needed and where the verification should happen. I know exposing a bot token or client secret in frontend JavaScript would be unsafe.

An AI suggested this flow: the frontend sends the user to Discord's OAuth authorization page, Discord redirects back to a Firebase Cloud Function, the function exchanges the authorization code and checks the user, then saves the result in Firestore. I'm not familiar with Cloud Functions or the best way to structure this.

I also want to save a user record, such as `{generalInfo, clients: {}}`, after login. Should that data be written by the frontend or by backend code? I'm considering Firebase for authentication and the database, but I'm not sure how all the pieces fit together.

3 Answers

Answered By CedarPixel41 On

You probably do not need a bot token just to let someone sign in with Discord. Use Discord OAuth with the smallest scope you need, usually `identify` (and possibly `email` if you genuinely need the email address). The user authorizes your application, Discord returns an authorization code, and your backend exchanges that code using the client secret. The secret must stay on the backend because visitors can inspect every value in browser code.

Answered By QuietHarbor8 On

That general flow is correct. The browser sends the user to Discord, Discord redirects back to a server-side Cloud Function, and the function handles the sensitive OAuth exchange and verification. A Cloud Function is basically backend code that runs when an HTTP request arrives, so it’s a reasonable option for a small project.

Keep the client secret and any tokens in the function’s environment or secret storage, never in frontend JavaScript. Once verification succeeds, the backend can create or update the user document in Firestore. The frontend should only receive a safe result or your own login session and then call your backend normally. Firebase Authentication plus Firestore is a perfectly reasonable place to start.

Answered By SilverNoodle6 On

For the database part, avoid trusting the frontend to write important account fields directly. A user can alter browser requests, so anything involving identity, permissions, credits, or ownership should be checked and written by backend code. The frontend can send ordinary profile updates to an API, but the API should validate them and decide what gets stored.

A simple structure would be: browser starts OAuth, Discord redirects to your Cloud Function, the function exchanges the code and verifies the Discord account, then it creates or updates the Firestore user document and establishes a session. Later, the frontend uses that session to access your own API. Firebase Security Rules can provide another layer of protection, but they should not be treated as a replacement for keeping secrets off the client.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.