firebase / firebase/firebase-admin-node

Various issues when used with Cloudflare Pages

Abierto
#2,069 41 comentarios 20 reacciones 1 asignado Reclamado por @lahirumaramba Ver en GitHub
api: auth type: feature request
Lenguaje dominante
TypeScript
Estrellas
1.7k
Forks
419
Merge medio
3 d 10 h
PR fusionados (30 d)
16

Descripción

### [READ] Step 1: Are you in the right place?

When running the following Typescript code in Cloudflare Pages on the server-side - in a Remix `server.ts` file:

```
import { applicationDefault, initializeApp as initializeAdminApp } from "firebase-admin/app";

initializeAdminApp({
credential: applicationDefault(),
})
```

I get the following error:

```
[pages:err] TypeError: globalThis.XMLHttpRequest is not a constructor
at checkTypeSupport (/private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:15751:17)
at node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js (/private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:15764:177)
at /private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:7:52
at node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/request.js (/private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:17874:5)
at /private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:7:52
at node-modules-polyfills:http (/private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:18033:5)
at /private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:7:52
at node-modules-polyfills-commonjs:http (/private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:18133:21)
at /private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:10:47
at node_modules/firebase-admin/lib/utils/api-request.js (/private/Users/jorrit/work/simpl/simpl-energy-website/functions/[[path]].js:22423:76)
```

This is caused by Cloudflare Workers only supporting _fetch_ not _XMLHttpRequest_.

Another issue I run into when using

```
import * as admin from "firebase-admin";
import { applicationDefault, initializeApp as initializeAdminApp } from "firebase-admin/app";

initializeAdminApp({
credential: applicationDefault(),
})

const adminAuth = admin.auth();
```

Is that I get the following error:

```
✘ [ERROR] Could not resolve "@firebase/database-compat/standalone"

node_modules/firebase-admin/lib/app/firebase-namespace.js:106:41:
106 │ ...ject.assign(fn, require('@firebase/database-compat/standalone'));
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The path "./standalone" is not currently exported by package "@firebase/database-compat":

node_modules/@firebase/database-compat/package.json:16:13:
16 │ "exports": {
╵ ^

None of the conditions provided ("types", "node") match any of the currently active conditions ("default", "require", "worker"):

node_modules/@firebase/database-compat/package.json:31:20:
31 │ "./standalone": {
╵ ^

Consider enabling the "types" condition if this package expects it to be enabled. You can use "conditions: ['types']" to do that:

node_modules/@firebase/database-compat/package.json:32:6:
32 │ "types": "./dist/database-compat/src/index.standalone.d.ts",
╵ ~~~~~~~

You can mark the path "@firebase/database-compat/standalone" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
```

Any chance this npm package can be made compatible with Cloudflare Pages/Workers?

### [REQUIRED] Step 2: Describe your environment

* Operating System version: Cloudflare Pages
* Firebase SDK version: 11.5.0
* Firebase Product: auth
* Node.js version: v19.6.0 (on my workstation, but not in Cloudflare Pages as it doesn't spin up a node environment)
* NPM version: 9.4.0

### [REQUIRED] Step 3: Describe the problem

#### Steps to reproduce:

```bash
npx create-remix@latest

? Where would you like to create your app? ./my-remix-app
? What type of app do you want to create? Just the basics
? Where do you want to deploy? Choose Remix App Server if you're unsure; it's easy to change deployment targets. Cloudflare Pages
? TypeScript or JavaScript? TypeScript
? Do you want me to run `npm install`? Yes

cd my-remix-app
npm install firebase
npm install firebase-admin
touch ./app/firebase.server.ts
```

Add the following content to `app/firebase.server.ts`:

```
import * as admin from "firebase-admin";
import { applicationDefault, initializeApp as initializeAdminApp } from "firebase-admin/app";
import { signInWithEmailAndPassword, getAuth } from "firebase/auth";

// errors with 'TypeError: globalThis.XMLHttpRequest is not a constructor'
initializeAdminApp({
credential: applicationDefault(),
})

// errors with 'Could not resolve "@firebase/database-compat/standalone"'
const adminAuth = admin.auth();

async function signIn(email: string, password: string) {
const auth = getAuth();
return signInWithEmailAndPassword(auth, email, password);
}

export { signIn }
```

And update the `app/routes/index.tsx` file to

```
import type { ActionArgs } from "@remix-run/cloudflare";
import { Form } from "@remix-run/react";
import { signIn } from "~/firebase.server";

export const action = async ({ request }: ActionArgs) => {
let formData = await request.formData();

let email = formData.get("email");
let password = formData.get("password");

const { user } = await signIn(email as string, password as string);
}

export default function Index() {
return (


Email



Password



Sign In


);
}
```

Now run `npm run dev` and see it fail.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.