-
Notifications
You must be signed in to change notification settings - Fork 4
Firebase Setup
Dae Houlihan edited this page Apr 10, 2025
·
4 revisions
This guide walks you through setting up Firebase for your project. This step is only necessary when you're ready to deploy your experiment or want to test with the actual Firestore database instead of the mock database.
- Go to the Firebase Console
- Click "Add project"
- Enter a project name for your experiment
- Choose whether to enable Google Analytics (optional)
- Click "Create project"
- In the Firebase Console:
- Click on "Build" in the left sidebar
- Select "Firestore Database"
- Click "Create Database"
- Choose security rules:
- Select "Start in production mode" (we'll update the rules later)
- Choose database location:
- Select the region closest to your participants
- Click "Enable"
- In the Firebase Console:
- Go to "Build" > "Authentication"
- Click "Get Started"
- Enable Anonymous authentication:
- Click on the "Anonymous" provider
- Toggle the switch to "Enable"
- Click "Save"
-
Register your web application:
- Go to Project Settings (gear icon in the top left)
- In the "Your apps" section, click the web icon (
</>) - Enter a nickname for your app (e.g., "My Experiment")
- Check "Also set up Firebase Hosting"
- Click "Register app"
-
Copy the Firebase configuration:
- You'll see a configuration object that looks like:
const firebaseConfig = { apiKey: 'your-api-key', authDomain: 'your-project-id.firebaseapp.com', projectId: 'your-project-id', storageBucket: 'your-project-id.appspot.com', messagingSenderId: 'your-sender-id', appId: 'your-app-id', measurementId: 'your-measurement-id', // if you enabled Analytics }
- Copy this configuration
-
Update your project credentials:
- Open
hosting/src/creds.tsin your project - Replace the
firebaseCredobject with your copied configuration - The file should look like:
import type { FirebaseOptions } from 'firebase/app' export const firebaseCred: FirebaseOptions = { apiKey: 'your-api-key', authDomain: 'your-project-id.firebaseapp.com', projectId: 'your-project-id', storageBucket: 'your-project-id.appspot.com', messagingSenderId: 'your-sender-id', appId: 'your-app-id', measurementId: 'your-measurement-id', // if you enabled Analytics } export const prolificCompletionCode: string = 'REPLACE-WITH-YOURS'
- Open
The template comes with Firestore security rules already configured in firestore.rules. These rules control access to your database.
-
Review the default rules in
firestore.rules:rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /sharedData/** { allow read: if true; allow write: if request.auth.uid == uid; } match /expData/{uid} { allow read: if true; allow write: if request.auth.uid == uid; } match /userData/{uid} { allow read: if true; allow write: if request.auth.uid == uid; } } } -
Deploy the rules to Firebase:
yarn deploy-rules
-
Login to Firebase CLI:
yarn firebase login
-
Initialize Firebase in your project:
yarn firebase init
- Select "Firestore" and "Hosting" features
- Choose your project from the list
- Accept the default Firestore rules file (
firestore.rules) - Accept the default Firestore indexes file (
firestore.indexes.json) - For hosting, use the default "hosting" directory for your public files
- Answer "Yes" when asked if you want to configure as a single-page app
-
Continue using the mock database for local development:
- Keep
simulateMockDatabase = trueinappConfig.ts - This uses the mock database adapter, which mimics Firestore functionality
- Keep
-
To test with the real Firestore database (optional):
- Change
simulateMockDatabase = falseinappConfig.ts - Start your development server:
yarn dev - Verify that data is being saved to Firestore by checking the Firebase Console
- Change
- Continue development with
yarn dev - When you're ready to deploy, follow the Deployment Guide
- Set up Prolific integration using the Data Collection guide