Basic Setup
Follow these steps if you're using Firebase Auth or Cloud Firestore
- Click the "Get Started" button
- Click the "Create a project" button
- Give your project a name and click "Continue"
- Toggle Google Analytics off for this project. If you'd like to use Google Analytics, we'll be setting this up separately so that we can configure it correctly for your Divjoy codebase.
- Click "Continue" after Firebase is done setting up your new project.
- Click the gear icon and then "Project settings"
- At the bottom of the screen click the "</>" icon to create a web app
- Name your web app. If your Firebase project was called "Lyft", you might name it "Lyft Web". Don't check the Firebase Hosting checkbox. Finally, click the "Register app" button.
- Click "Continue to console". You don't need to copy and paste these scripts into your code. We'll have you add the required values as environment variables in the next step.
.env
file.- Go to project settings. Scroll down to the "Your Apps" section and copy the values for
projectId
,apiKey
, andauthDomain
into your codebase.env
file.
Your .env
file should now look like this:
You also need to add these values to the bottom section of your .env
. This section determines what values are included in your client-side JS. By specifying them separately we ensures that any values we don't want to expose (such as private keys) are kept server-side only.
- Click the "Service accounts" tab under the big "Project settings" header and then click the "Generate new private key" button at the bottom.
- Click the "Generate key" button
private_key
and client_email
values into your codebase .env
file. This is necessary for any server logic that needs to talk to Firebase.- The file should look something like this. You need to copy the entire value between the quotes, including the
----BEGIN PRIVATE KEY-----\n
and\n-----END PRIVATE KEY-----\n
portions.
Your .env
file should now look like this:
Firebase Auth
Follow these steps if you're using Firebase Auth (skip if using a different auth provider)
- Click "Build" then "Authentication in the left-hand column
- If you see this introductory screen you'll need to click "Get started"
- Go to the "Sign-in method" tab
- Click "Email/Password", toggle the "Enable" switch, and click "Save".
- For example, to get Twitter auth working you'll need to go to https://developer.twitter.com, create an app, then fill in the API Key and API secret.
http://localhost:3000/firebase-action
. This page is automatically inserted by Divjoy when you export your codebase.- Go to the "Templates" tab and then click the edit button (pencil icon) on the right side.
- Click the small "Customize action URL" link at the bottom.
- Add a URL that points to the /firebase-action page in your codebase and then click "Save". For testing this locally you'll want to use your localhost URL. You can change this later to your production URL or have a separate Firebase app for production.
- Important: If you're using Cloud Firestore then authentication will not work until that is setup. The auth logic we give you in
src/util/auth.js
automatically merges extra user data from Cloud Firestore and will fail if it's unreachable. Continue on to the next section to setup Cloud Firestore.
Cloud Firestore
Follow these steps if you're using Cloud Firestore (skip if using a different database provider).
- Click the "Cloud Firestore" link on the left and then click "Create database".
- Select "Start in test mode" and then click the "Next" button. We'll give you the rules to secure your database in a following step.
- Pick the Cloud Firestore location closest to your servers and then click "Enable". If not sure, then just go with the default.
firestore.rules
file. This ensures that your users cannot read or edit data that they shouldn't have access to.- It should look like this after pasting in the contents of your
firestore.rules
file. Click the "Publish" button to push these new rules live.
Just click "Get started" and then you're done with this step
items
collection on the fields owner (ascending)
and createdAt (descending)
. This is required so that your useItemsByOwner
function can properly fetch a user's items and sort by date created. You'll see an example UI that utilizes this database logic in the dashboard page of your codebase.- Click the "+ Create index" link
- Fill in the form as you see below and then click the "Create index" button. Remember that the
createdAt
field should beDescending
. - After creating the index you'll notice it says it has a status of "Building". You're safe to close this page and continue setting up your app. It should switch to "Enabled" in a few minutes.
.env
file.FAQ
If you're using Cloud Firestore as your database provider then the auth logic we give you in src/util/auth.js
automatically merges extra user data from Cloud Firestore and will fail if it's unreachable (causing it to be stuck in a loading state). You can check as to whether this is the issue by temporarily setting const MERGE_DB_USER = false
in src/util/auth.js
. To resolve this issue you'll want to follow the Cloud Firestore instructions above. After the setup, make sure you delete any account from Firebase Authentication that you've created while encountering this bug.
Make sure that all environment variables for all the services in your .env
file have values filled in. Also make sure the variables at the bottom (starting with REACT_
or NEXT_
) have values filled in. Ensure the values are correctly formatted by comparing them to what you see in this screenshot (for example the `authDomain` value shouldn’t start with "https://").
You can disable this by going to src/util/auth.js
and setting const EMAIL_VERIFICATION
to false
.
Assuming you are using the authentication components from the Divjoy library, you just need to go to src/pages/auth.js
and update the providers array on the AuthSection
component. For example, here's what it would look like if you want to display options for Google, Facebook, and Twitter.
Our authentication component supports the following by default: google
, facebook
, twitter
, and github
. You can add more options by configuring them in Firebase and then following this guide to display them in your component: Adding new login providers to Firebase.