How to setup a Firebase demo project
Asked Answered
I

3

32

The Firebase documentation mentions that the local emulator supports Real and Demo projects. A demo project is defined as:

A demo project has no Firebase console configuration and no live resources. Demo project IDs have the demo- prefix.

The above mentions a demo- prefix but I can't find any examples of how that works. Is it possible to setup a demo project as specified in the above documentation without requiring a Firebase console project or any live resources?

Interoceptor answered 1/6, 2021 at 1:0 Comment(1)
I don't get this either. It sounds like there should be a way to add a demo project with firebase use --add. I've tried manually adding a demo project to .firebaserc to no avail.Superiority
G
34

You can start the emulators with a demo project by using the --project flag:

$ firebase emulators:start --project demo-test --only auth

Just make sure that the project ID start with the demo- prefix. This line will produce the following output:

i  emulators: Starting emulators: auth
i  emulators: Detected demo project ID "demo-test", emulated services will use a demo configuration and attempts to access non-emulated services for this project will fail.
Genu answered 21/7, 2021 at 8:32 Comment(6)
Do you have to init a project called demo-test first?Calefaction
Ow good old google docs... adding this line avoids so much confusion...Kelsiekelso
This works like a charm, but note that you can no longer rely on the default project specified by firebase use and you need to specify the demo project on every CLI command (e.g. firebase functions:shell --project demo-test)Amplifier
Where do you an API key to initialize the Firebase client library? Do you just make up a random one?Colton
@Colton you can use a random one. I prefer to use a string any so it's clear that API key can be any non-empty string. Also don't forget to call useAuthEmulator('localhost', 9099) in your code.Lucic
when i try this my cloud functions break: functions: Failed to load function definition from source: FirebaseError: HTTP Error: 403, Permission denied on resource project demo-lendus. but everything else works fine. Any suggestions?Sumerian
O
1

Create folder for project, initialize firebase with command firebase init. When initialization process ask you for project to use, select last option: Don't set up a default project.

I was got one error message at end of initialization process because region didn't set, but demo app should work fine.

Select what you need for project. After that go to file .firebaserc and write this into it:

    {
         "projects": {
             "default": "demo-project"
          }
     }

I successfully ran firebase with:

    import { initializeApp } from 'firebase/app'
    import { getFirestore, connectFirestoreEmulator } from 'firebase/firestore'

    const app = initializeApp({
         apiKey: 'any',
         authDomain: 'any',
         projectId: 'demo-project', // project name from .firebaserc
         storageBucket: 'any',
         messagingSenderId: 'any',
         appId: 'any',
    })
    export const db = getFirestore() // no need for app object here
    connectFirestoreEmulator(db, '127.0.0.1', 8080)

Run emulators with: firebase emulators:start

Oppidan answered 24/12, 2023 at 14:30 Comment(0)
S
0

Swift app developers using Xcode may find this useful. I am building a demo app in Swift to test Firebase's APIs using emulators only without creating an actual Firebase project. In other words, I'm making a pure demo app, per Firebase's recommendation.

I struggled with getting a working setup by modifying the GoogleService-Info.plist typically used by the Firebase Swift SDK. Building on @easyScript's answer and doing some research, I found I could get it working with no GoogleService-Info.plist by using FirebaseApp configure(options:).

Note that while the googleAppId is not real, it must be in the correct format. I tried using arbitrary strings and they caused an exception. Also, the projectID must start with demo-, as documented.

Here's my working setup code for connecting to the Auth and Firestore emulators without creating a Firebase project:

let options = FirebaseOptions(googleAppID: "1:123:ios:123abc",
                              gcmSenderID: "123456789")
options.apiKey = "any"
options.projectID = "demo-firebase-explorer"
options.bundleID = Bundle.main.bundleIdentifier!
FirebaseApp.configure(options: options)

Auth.auth().useEmulator(withHost:"localhost", port:9099)
Firestore.firestore().useEmulator(withHost: "localhost", port: 8080)

let settings = Firestore.firestore().settings
settings.isSSLEnabled = false
Firestore.firestore().settings = settings

It's also worth noting that if your target is macOS and you are running in a sandbox, you will need to enable 'Outgoing Connections (Client)' under 'Signing & Capabilities' -> 'App Sandbox'

Susceptive answered 4/6, 2024 at 23:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.