Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. #80

Open
austin3410 opened this issue Sep 29, 2024 · 1 comment
Assignees

Comments

@austin3410
Copy link

austin3410 commented Sep 29, 2024

Describe the issue
On a new setup, I can't perform some social tasks like messaging friends and sharing recipes with friends. When checking the API logs I see:

POST /messages?token=c5d59851c1d664cff2f6a1c19b75468ce6a35e72ec2c648fd79bc55925e78e97f46b5da36a1a72ffca2a593dfb321269& 500 53.625 ms - 1249
Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
    at AppStore.getApp (/app/node_modules/firebase-admin/lib/app/lifecycle.js:65:19)
    at FirebaseNamespaceInternals.app (/app/node_modules/firebase-admin/lib/app/firebase-namespace.js:54:35)
    at FirebaseNamespace.app (/app/node_modules/firebase-admin/lib/app/firebase-namespace.js:256:30)
    at FirebaseNamespace.ensureApp (/app/node_modules/firebase-admin/lib/app/firebase-namespace.js:266:24)
    at FirebaseNamespace.messaging (/app/node_modules/firebase-admin/lib/app/firebase-namespace.js:114:25)
    at Object.sendMessage (/app/packages/backend/src/services/firebase.js:22:6)
    at map (/app/packages/backend/src/services/firebase.js:11:35)
    at Array.map (<anonymous>)
    at Object.sendMessages (/app/packages/backend/src/services/firebase.js:11:12)
    at Object.dispatchMessageNotification (/app/packages/backend/src/services/util.js:97:23)

I'm unfamiliar with Firebase so I have no clue which container this pertains to or what I should look into.
docker-compose:

# This config file is for RecipeSage-selfhost version 4.2.0, and RecipeSage version 2.15.3.

services:
  proxy: # The proxy must not be removed. If needed, point your own proxy to this container, rather than removing this
    container_name: recipesage_proxy
    image: julianpoy/recipesage-selfhost-proxy:v4.0.0
    ports:
    - 7270:80
    depends_on:
      - static
      - api
      - pushpin
    restart: unless-stopped
  static: # Hosts frontend assets
    container_name: recipesage_static
    image: julianpoy/recipesage-selfhost:static-v2.15.3
    restart: unless-stopped
  api: # Hosts backend API
    container_name: recipesage_api
    image: julianpoy/recipesage-selfhost:api-v2.15.3
    depends_on:
      - postgres
      - typesense
      - pushpin
      - browserless
    command: sh -c "npx prisma migrate deploy; npx nx seed prisma; npx ts-node --swc --project packages/backend/tsconfig.json packages/backend/src/bin/www"
    environment:
      - STORAGE_TYPE=filesystem
      - FILESYSTEM_STORAGE_PATH=/rs-media
      - NODE_ENV=selfhost
      - VERBOSE=false
      - VERSION=selfhost
      - POSTGRES_DB=recipesage_selfhost # If changing this, make sure to update the postgres container and the DATABASE_URL below accordingly
      - POSTGRES_USER=recipesage_selfhost # If changing this, make sure to update the postgres container and the DATABASE_URL below accordingly
      - POSTGRES_PASSWORD=[REDACTED] # If changing this, make sure to update the postgres container and the DATABASE_URL below accordingly
      - POSTGRES_PORT=5432 # If changing this, make sure to update the postgres container and the DATABASE_URL below accordingly
      - POSTGRES_HOST=postgres # If changing this, make sure to update the postgres container and the DATABASE_URL below accordingly
      - POSTGRES_SSL=false
      - POSTGRES_LOGGING=false
      - DATABASE_URL=postgresql://recipesage_selfhost:[REDACTED]@postgres:5432/recipesage_selfhost # Format is: postgresql://USERNAME:PASSWORD@HOST:PORT/DBNAME and must match the variables from the postgres container if you decide to change them
      - GCM_KEYPAIR
      - SENTRY_DSN
      - GRIP_URL=http://pushpin:5561/
      - GRIP_KEY=[REDACTED]
      - SEARCH_PROVIDER=typesense
      - 'TYPESENSE_NODES=[{"host": "typesense", "port": 8108, "protocol": "http"}]'
      - TYPESENSE_API_KEY=[REDACTED]
      - STRIPE_SK # Value should not be set.
      - STRIPE_WEBHOOK_SECRET # Value should not be set
      - BROWSERLESS_HOST=browserless
      - BROWSERLESS_PORT=3000
      - INGREDIENT_INSTRUCTION_CLASSIFIER_URL=http://localhost:3000/
      - OPENAI_API_KEY=[REDACTED] # Please follow the instructions in the README if you decide to supply a value here
    volumes:
      - apimedia:/rs-media
    restart: unless-stopped
  typesense: # Provides the fuzzy search engine
    container_name: recipesage_typesense
    image: typesense/typesense:0.24.1
    ports:
    - 8108:8108
    volumes:
      - typesensedata:/data
    command: "--data-dir /data --api-key=[REDACTED] --enable-cors"
    restart: unless-stopped
  pushpin: # Provides websocket support
    container_name: recipesage_pushpin
    image: julianpoy/pushpin:2023-09-17
    entrypoint: /bin/sh -c
    command:
      [
        'sed -i "s/sig_key=[REDACTED]]/sig_key=$$GRIP_KEY/" /etc/pushpin/pushpin.conf && echo "* $${TARGET},over_http" > /etc/pushpin/routes && pushpin --merge-output',
      ]
    environment:
      - GRIP_KEY=[REDACTED]
      - TARGET=api:3000
    restart: unless-stopped
  postgres: # Database
    container_name: recipesage_postgres
    image: postgres:16
    environment:
      - POSTGRES_DB=recipesage_selfhost # If you change this, make sure to change both POSTGRES_DB and DATABASE_URL on the API container
      - POSTGRES_USER=recipesage_selfhost # If you change this, make sure to change both POSTGRES_USER and DATABASE_URL on the API container
      - POSTGRES_PASSWORD=[REDACTED] # If you change this, make sure to change both POSTGRES_PASSWORD and DATABASE_URL on the API container
    volumes:
      - postgresdata:/var/lib/postgresql/data
    restart: unless-stopped
  browserless: # A headless browser for scraping websites with the auto import tool
    container_name: recipesage_browserless
    image: browserless/chrome:1.61.0-puppeteer-21.4.1
    environment:
      - MAX_CONCURRENT_SESSIONS=3
      - MAX_QUEUE_LENGTH=10
    restart: unless-stopped

  # Uncomment the lines below to enable the ingredient instruction classifier. It's somewhat resource intensive (and large) and requires AVX instruction support on your CPU, but does improve site compatibility for the automatic import tool.

  # ingredient-instruction-classifier: # An _optional_ server that runs a machine learning model able to classify text for improved automatic import site support. Not required for application to run, and will crash if your CPU does not support AVX instructions
  #   container_name: recipesage_classifier
  #   image: julianpoy/ingredient-instruction-classifier:1.4.11
  #   environment:
  #     - SENTENCE_EMBEDDING_BATCH_SIZE=200
  #     - PREDICTION_CONCURRENCY=2
  #   restart: unless-stopped

volumes:
  apimedia:
    driver: local
  typesensedata:
    driver: local
  postgresdata:
    driver: local

Please do the following and check the boxes before filing
[x ] I have attached my docker-compose.yaml to the issue
[x ] I have attached the logs for my api container to the issue
[x ] My issue is related to this selfhost repo, not the main tracker for RecipeSage here

@austin3410
Copy link
Author

Found a possible fix.

Added:
admin.initializeApp()

to file /app/packages/backend/src/services/firebase.js

So now it reads:

import { admin } from "./firebase-admin";
import * as Sentry from "@sentry/node";

admin.initializeApp()
                                              
// DB                                         
import { FCMToken } from "../models/index.js";                              
                                                                            
let invalidFcmTokenErrors = ["messaging/registration-token-not-registered"];
                                                  
export const sendMessages = (tokens, payload) => {             
return Promise.all(                                          
    tokens.map((token) => exports.sendMessage(token, payload)),
  );
};                                              
                                                
export const sendMessage = (token, payload) => {
const message = {
  data: payload,
  token,
};          
              
return admin    
.messaging()     
.send(message)                                                 
.catch((err) => {                                              
if (invalidFcmTokenErrors.indexOf(err.errorInfo.code) > -1) {
return FCMToken.destroy({
where: {
    token,
    },  
  });                          
} else {                       
  Sentry.captureException(err);
}                        
                           
      return Promise.resolve();
    });
};
~

After restarting the container I can now send messages, and share recipes to other users.
I don't know what other implications this has so if this is a bad idea please let me know. For now though everything seems to be working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants