Firebase Admin SDK with Netlify server side lambda

Hello

I have an SSR react app (Express) and my lambda functions work fine and all but connecting to the Firebase Real-Time database is a struggle. Google many answers which of none works.

I get several errors such as init standalone is not a function and “IDBIndex is not defined’,”

here is my code: (lamda fn)

import * as admin from 'firebase-admin'

const serviceAccount = require('../../my-saas-XXX.json')

admin.initializeApp({

  credential: admin.credential.cert(serviceAccount),

  databaseURL: 'https://my-saas.firebaseio.com',

})

export async function handler(event, context) {

  const body = JSON.parse(event.body)

  const {

    email,

    password,

    name,

    organisation,

    logo,

    phone,

    address,

    homepage,

    option,

  } = body

  console.log('body:::::::::::::::::::', body)

  const user = {

    email,

    name,

    organisation,

    logo,

    phone,

    address,

    homepage,

    option,

    admin: false,

    key_li: '',

    key_ga: '',

  }

  const db = admin.database()

  // console.log(db)

  console.log('user', user)

  return await admin

    .auth()

    .createUser({

      email: email,

      emailVerified: false,

      phoneNumber: phone,

      password: password,

      displayName: name,

      photoURL: 'http://www.example.com/12345678/photo.png',

      disabled: false,

    })

    .then((usr) => {

      db.ref('users/' + usr.uid)

        .set(user)

        .then(console.log('uid', usr.uid))

        .catch((error) => {

          console.log(error.message)

        })

      return {

        statusCode: 200,

        body: JSON.stringify({ data: usr.uid }),

      }

    })

    .catch((e) => {

      return {

        statusCode: 500,

        body: JSON.stringify({ data: e.message }),

      }

    })

}

I added a webpack.config.js which stance

module.exports = {
  resolve: {
    mainFields: ['main', 'module'],
  },
}

Hi @Mik-A,

I’ve seen a few people in this community struggle to use firebase-admin, as well. That said, can you provide some more insight on how you are deploying your function? Are you bundling your function using netlify-lambda? Or are you self-bundling like I did here: function-deploy-test/package.json at master · netlify/function-deploy-test · GitHub?

@Dennis I’m building with Netlify-cli
the firebase-admin workswhen it comes to handle users, it does not work when doing something with the databases. This is a pity since it ias much more convenient than the Rest API. That one has only post, put, patch and delete

The whole point of having Netlify lambda is starting to go avain.

Hey @Mik-A,
Not sure if you’ve given up on firebase-admin yet, but wanted to share a code snippet in case it’s helpful :slight_smile:

Is ok this work for me thanks :smiley: @jen