see it in action here!
Firebase provides great structure to create your own authentication system. However, the default providers list for external log-ins (oAuth2) is limited.
I'm a big fan of Discord and so here is a sample Firebase app that uses the Discord oAuth2 to create users in our Firebase Authentication and our Firestore database.
Please read the Disclaimer below if you have any issues.
If you need any help or assistance open an issue/pull request here on GitHub.
- User heads to
/api/login
and is redirected to Discord - User signs in with their Discord credentials and authorizes our app
- Discord returns the user information to our
/api/auth
callback URI - Using the information returned, we create Firebase user object in the Authentication tab
- A document in a Firestore database is also created inside a
users
collection
- Go to the Discord Developer Portal
- Create an application
- Make note of your
Client ID
andClient Secret
- Go into the OAuth2 tab and add the following redirect URIs:
https://us-central1-<project-id>.cloudfunctions.net/api
https://us-central1-<project-id>.cloudfunctions.net/api/auth
- hit save!
- Make note of the scopes you want from the bottom of the page
- Go to the Firebase Console
- Create your project and head to the
Authentcation
tab - Enable Email/Password authentication
- Go to the
Database
tab and enableFirestore Cloud
storage, more info on how to do this here - Click the settings gear next to Project Overview and select Web, add a nickname and get your
Config
object. If you are unsure what to do, follow the steps linked here.
- Open the
info.js
folder inside thefunctions
folder and replace all information in there with the information you got from Discord/Firebase - Open the
.firebasesrc
file and fill in your project name. Note: if you do not see this file you must enable the visibility of hidden files on your operating system - CD into the
functions
folder and saynpm install
to install al necessary packages - Make sure you have Firebase Tools installed globally by saying
npm install -g firebase-tools
- Inside your project directory say
firebase deploy
- You should be prompted to login with Google, make sure you use the same account you used to setup your Firebase project in the steps above
- Your project should be live! Go to
https://us-central1-<project-id>.cloudfunctions.net/api/login
to see it in action - Head over to Firebase and check out your users being added to the
Authentication
Tab - Head over to your Firestore Database and notice how we store additional user information that the Firebase authentication system does not let us store, for example, a user's profile picture, joined servers, and more.
If you are getting errors because of your redirect URL, check that the URL listed in your Discord Developer Portal matches the URL of your project.