Skip to content

Commit 56d77ba

Browse files
authored
feat(examples/firebase): deploy to Cloud Functions for Firebase (#53)
1 parent 9b3cccf commit 56d77ba

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

firebase/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ node_modules
88
firestore-debug.log
99
firebase-debug.log
1010
ui-debug.log
11+
.firebase

firebase/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ See the screen recording at `./screen_recording.gif` or Open this example on [Co
1010

1111
[![Open in CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/remix-run/examples/tree/main/firebase)
1212

13-
## Example
13+
## Running locally
1414

1515
To run it, you need to either:
1616

@@ -31,15 +31,25 @@ When the SERVICE_ACCOUNT and CLIENT_CONFIG environment variables have not been s
3131

3232
When you run `npm run emulators`, an initial user is created with credentials `[email protected]:password`. This can be configured in `firebase-fixtures/auth/accounts.json` or via the emulator UI.
3333

34-
## Auth (`app/server/auth.server.ts`)
34+
## Deploying
35+
36+
1. Follow the "Run against a Firebase Project" steps above if not done already
37+
2. Install the Firebase CLI with `npm i -g firebase-tools`
38+
3. Log in to the CLI with `firebase login`
39+
4. Run `firebase use --add` and choose the Firebase project you want to deploy to
40+
5. Deploy with `firebase deploy`
41+
42+
## Details
43+
44+
### Auth (`app/server/auth.server.ts`)
3545

3646
`signIn` returns a Firebase session-cookie-string, when sign-in is successfull. Then Remix `cookieSessionStorage` is used to set, read and destroy it.
3747

3848
`signUp` creates a user and calls sign-in to receive the session cookie.
3949

4050
`requireAuth` uses `firebase-admin` to verify the session cookie. When this check fails, it throws a `redirect` to the login page. Use this method to protect loaders and actions. The returned `UserRecord` can be handy to request or manipulate data from the Firestore for this user.
4151

42-
## Firestore (`app/server/db.server.ts`)
52+
### Firestore (`app/server/db.server.ts`)
4353

4454
Requests to the Firestore are made using the `firebase-admin`-SDK. You need to check validity of your requests manually, since `firestore.rules` don't apply to admin requests.
4555

firebase/firebase.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
{
2+
"functions": {
3+
"predeploy": "npm run build",
4+
"source": "."
5+
},
6+
"hosting": {
7+
"public": "public",
8+
"ignore": [
9+
"firebase.json",
10+
"**/.*"
11+
],
12+
"rewrites": [{
13+
"source": "**",
14+
"function": "remix"
15+
}]
16+
},
217
"emulators": {
18+
"functions": {
19+
"port": 5001
20+
},
21+
"hosting": {
22+
"port": 5000
23+
},
324
"auth": {
425
"port": 9099
526
},

firebase/functions.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
specVersion: v1alpha1
2+
endpoints:
3+
remix:
4+
platform: gcfv2
5+
entryPoint: remix
6+
httpsTrigger: {}

firebase/functions/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { onRequest } = require("firebase-functions/v2/https");
2+
const { createRequestHandler } = require("remix-google-cloud-functions");
3+
4+
const remix = onRequest(
5+
createRequestHandler({
6+
build: require("../build"),
7+
})
8+
);
9+
module.exports = { remix };

firebase/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"private": true,
33
"sideEffects": false,
4+
"main": "./functions/index.js",
45
"scripts": {
56
"build": "remix build",
67
"dev": "remix dev",
@@ -13,9 +14,11 @@
1314
"@remix-run/react": "*",
1415
"@remix-run/serve": "*",
1516
"firebase-admin": "^10.0.2",
17+
"firebase-functions": "^3.21.2",
1618
"isbot": "^3.6.5",
1719
"react": "^18.2.0",
18-
"react-dom": "^18.2.0"
20+
"react-dom": "^18.2.0",
21+
"remix-google-cloud-functions": "0.0.1"
1922
},
2023
"devDependencies": {
2124
"@remix-run/dev": "*",
@@ -27,6 +30,6 @@
2730
"typescript": "^4.8.4"
2831
},
2932
"engines": {
30-
"node": ">=14"
33+
"node": "14"
3134
}
3235
}

0 commit comments

Comments
 (0)