-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(global): add firebase emulator script
- Loading branch information
Showing
7 changed files
with
86 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
{ | ||
"private": true, | ||
"name": "@riva/functions", | ||
"version": "1.0.0", | ||
"main": "lib/index.js", | ||
"module": "lib/index.js", | ||
"packageManager": "pnpm@8.9.0", | ||
"packageManager": "[email protected].1", | ||
"engines": { | ||
"node": ">=20" | ||
"node": "20" | ||
}, | ||
"scripts": { | ||
"lint": "eslint --ext .js,.ts .", | ||
"dev": "firebase emulators:start --import=firebase-export --export-on-exit --only auth,functions,firestore,storage", | ||
"build": "tsc", | ||
"build:watch": "tsc --watch", | ||
"serve": "npm run build && firebase emulators:start --only functions", | ||
|
@@ -18,6 +20,7 @@ | |
"logs": "firebase functions:log" | ||
}, | ||
"dependencies": { | ||
"@riva/shared": "workspace:^", | ||
"firebase-admin": "^11.8.0", | ||
"firebase-functions": "^4.3.1", | ||
"mime": "^4" | ||
|
@@ -29,6 +32,7 @@ | |
"eslint-config-google": "^0.14.0", | ||
"eslint-plugin-import": "^2.25.4", | ||
"firebase-functions-test": "^3.1.0", | ||
"isolate-package": "^1.13.2", | ||
"typescript": "^4.9.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { FSUser } from '@riva/shared'; | ||
|
||
import { FieldValue } from 'firebase-admin/firestore'; | ||
|
||
import { logger } from 'firebase-functions/v2'; | ||
import { beforeUserCreated } from 'firebase-functions/v2/identity'; | ||
|
||
import { userRef } from '../references'; | ||
|
||
export const authBeforeUserCreated = beforeUserCreated(async (event) => { | ||
logger.log(event); | ||
|
||
const { | ||
data: { | ||
uid, | ||
email = null, | ||
emailVerified, | ||
displayName = null, | ||
photoURL = null, | ||
}, | ||
} = event; | ||
|
||
const fsUser: FSUser = { | ||
uid: uid, | ||
email: email, | ||
email_verified: emailVerified, | ||
display_name: displayName, | ||
photo_url: photoURL, | ||
is_onboarded: false, | ||
is_suspended: false, | ||
updated_at: FieldValue.serverTimestamp(), | ||
created_at: FieldValue.serverTimestamp(), | ||
}; | ||
|
||
try { | ||
logger.log(fsUser); | ||
await userRef(uid).set(fsUser); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
|
||
return {}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getApp, getApps, initializeApp } from 'firebase-admin/app'; | ||
import { getAuth } from 'firebase-admin/auth'; | ||
import { getStorage } from 'firebase-admin/storage'; | ||
import { getFirestore } from 'firebase-admin/firestore'; | ||
import { getFunctions } from 'firebase-admin/functions'; | ||
|
||
/** | ||
* Singleton Firebase app instance | ||
* | ||
* @remarks | ||
* This is to prevent hot Cloud Functions from crashing | ||
*/ | ||
const app = getApps().length ? getApp() : initializeApp(); | ||
|
||
export const auth = getAuth(app); | ||
export const storage = getStorage(app); | ||
export const firestore = getFirestore(app); | ||
export const functions = getFunctions(app); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
/** | ||
* Import function triggers from their respective submodules: | ||
* | ||
* import {onCall} from "firebase-functions/v2/https"; | ||
* import {onDocumentWritten} from "firebase-functions/v2/firestore"; | ||
* | ||
* See a full list of supported triggers at https://firebase.google.com/docs/functions | ||
* Initialise Firebase Admin SDK | ||
*/ | ||
import './firebase'; | ||
import { setGlobalOptions } from 'firebase-functions/v2'; | ||
|
||
import { onRequest } from 'firebase-functions/v2/https'; | ||
import * as logger from 'firebase-functions/logger'; | ||
|
||
// Start writing functions | ||
// https://firebase.google.com/docs/functions/typescript | ||
|
||
export const helloWorld = onRequest((_, response) => { | ||
logger.info('Hello logs!', { structuredData: true }); | ||
response.send('Hello from Firebase!'); | ||
setGlobalOptions({ | ||
maxInstances: 1, | ||
concurrency: 1000, | ||
timeoutSeconds: 60, | ||
region: 'asia-northeast1', | ||
}); | ||
|
||
export * from './auth/beforeUserCreated'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { firestore } from './firebase'; | ||
|
||
const { doc } = firestore; | ||
|
||
export const userRef = (uid: string) => doc(`users/${uid}`); | ||
export const memberRef = (uid: string) => doc(`members/${uid}`); | ||
export const eventRef = (uid: string) => doc(`events/${uid}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters