Skip to content

Commit

Permalink
feat(global): add firebase emulator script
Browse files Browse the repository at this point in the history
  • Loading branch information
taronaeo committed May 9, 2024
1 parent fe2ecbb commit 32cd1e1
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 21 deletions.
4 changes: 1 addition & 3 deletions portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"type": "module",
"name": "@riva/portal",
"scripts": {
"dev": "run-p dev:web dev:emulator",
"dev:web": "vite dev",
"dev:emulator": "firebase emulators:start --import=firebase-export --export-on-exit --only auth,firestore,storage",
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"test": "npm run test:integration && npm run test:unit",
Expand Down
8 changes: 6 additions & 2 deletions services/functions/package.json
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",
Expand All @@ -18,6 +20,7 @@
"logs": "firebase functions:log"
},
"dependencies": {
"@riva/shared": "workspace:^",
"firebase-admin": "^11.8.0",
"firebase-functions": "^4.3.1",
"mime": "^4"
Expand All @@ -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"
}
}
43 changes: 43 additions & 0 deletions services/functions/src/auth/beforeUserCreated.ts
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 {};
});
18 changes: 18 additions & 0 deletions services/functions/src/firebase.ts
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);
25 changes: 10 additions & 15 deletions services/functions/src/index.ts
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';
7 changes: 7 additions & 0 deletions services/functions/src/references.ts
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}`);
2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "!.next/cache/**"]
"outputs": [".svelte-kit/**"]
},
"lint": {
"dependsOn": ["^lint"]
Expand Down

0 comments on commit 32cd1e1

Please sign in to comment.