Welcome to the ultimate guide on integrating Discord OAuth2 authentication with Firebase! Leveraging Discord’s robust authentication system allows you to create a seamless user experience while managing your user data effectively through Firebase’s database structure. Here’s how you can set it up effortlessly.
What is the OAuth2 Flow?
Imagine you own a café, and you want customers to have the option to register with their social media accounts. Instead of having them fill out a lengthy form, they can simply walk in, show proof of their identity via their social media (Discord in this case), and get a membership card instantly. That’s precisely what OAuth2 does—it simplifies the authentication process, allowing users to sign in using their existing accounts.
How Does It Work?
- User navigates to api/login and is redirected to Discord.
- User signs in with their Discord credentials and authorizes your app.
- Discord returns the user information to your apiauth callback URI.
- A user object is then created in the Firebase Authentication tab.
- A corresponding document is created in Firestore database inside the users collection.
How to Run This Integration
Setting Up Discord
- Go to the Discord Developer Portal.
- Create a new application.
- Make a note of your Client ID and Client Secret.
- In the OAuth2 tab, add the following redirect URIs:
- https://us-central1-project-id.cloudfunctions.net/api
- https://us-central1-project-id.cloudfunctions.net/apiauth
- Hit save!
- Take note of the scopes you want from the bottom of the page.
Setting Up Firebase
- Go to the Firebase Console.
- Create a new project and navigate to the Authentication tab.
- Enable Email/Password authentication.
- Go to the Database tab and enable Firestore Cloud storage. Find more information on how to do this here.
- Click the settings gear next to Project Overview, select Web, add a nickname and retrieve your Config object. Follow the steps linked here if unsure.
Setting Up Our Code
- Open the info.js file inside the functions folder and replace all information in there with the information from Discord and Firebase.
- Open the .firebasesrc file and fill in your project name. Note: You may need to enable the visibility of hidden files on your operating system if you don’t see this file.
- CD into the functions folder and run npm install to install all necessary packages.
- Ensure you have Firebase Tools installed globally by running npm install -g firebase-tools.
- Within your project directory, execute firebase deploy.
- You should be prompted to log in with Google. Make sure to use the same account you set up your Firebase project with.
- Your project should be live! Visit https://us-central1-project-id.cloudfunctions.net/apilogin to see it in action.
- Check out your users in the Firebase Authentication Tab.
- Inspect your Firestore Database to see additional user information, such as profile pictures and joined servers.
Troubleshooting
If you encounter any issues, especially related to redirect URLs, ensure that the URLs listed in your Discord Developer Portal perfectly match the URLs of your project. It’s a good practice to double-check these settings.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

