Skip to content

Commit

Permalink
feat: refactor firebase website and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vighnesh153 committed Dec 2, 2024
1 parent e61e79c commit 1504df7
Show file tree
Hide file tree
Showing 220 changed files with 21,348 additions and 13,319 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ jobs:

# Check if all nodejs projects are ok
- run: |
export PIKA_PATH="$(pwd)";
for dir in */; do
cd $PIKA_PATH/$dir;
echo "Checking - $(pwd)\n";
npm install;
npm t;
npm run build;
done
cd vighnesh153-astro/website;
npm install --save-exact;
npm t;
npm run build;
5 changes: 5 additions & 0 deletions tools-nodejs/vighnesh153-astro/.firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "vighnesh153-app"
}
}
5 changes: 3 additions & 2 deletions tools-nodejs/vighnesh153-astro/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ pnpm-debug.log*
.DS_Store
*.pem

.sst
sst-env.d.ts
cdk.context.json
tsconfig.tsbuildinfo

Expand All @@ -40,3 +38,6 @@ playwright/.cache

# deno
deno.json

# firebase
.firebase
1 change: 0 additions & 1 deletion tools-nodejs/vighnesh153-astro/README.md

This file was deleted.

4 changes: 4 additions & 0 deletions tools-nodejs/vighnesh153-astro/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const firebaseCollections = {
usersByUserId: "users_by_userId",
userIdByUsername: "userId_by_username",
};
29 changes: 29 additions & 0 deletions tools-nodejs/vighnesh153-astro/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"hosting": {
"source": "./website",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"frameworksBackend": {
"region": "asia-east1"
}
},
"functions": [
{
"source": "./functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log",
"*.local"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"engines": {
"node": "18"
},
"main": "lib/index.js",
"main": "lib/functions/src/index.js",
"dependencies": {
"firebase-admin": "^12.6.0",
"firebase-functions": "^6.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@

import * as logger from "firebase-functions/logger";
import { HttpsError } from "firebase-functions/v2/https";
import { beforeUserCreated } from "firebase-functions/v2/identity";
import {
beforeUserCreated,
beforeUserSignedIn,
} from "firebase-functions/v2/identity";

import { initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";

import { firebaseCollections } from "../../constants";

initializeApp();

function slugify(text: string): string {
Expand All @@ -23,7 +28,7 @@ function slugify(text: string): string {
);
}

export const beforecreated = beforeUserCreated(async (event) => {
export const createUserRecords = beforeUserCreated(async (event) => {
const firestore = getFirestore();

const user = event.data;
Expand All @@ -38,6 +43,11 @@ export const beforecreated = beforeUserCreated(async (event) => {
throw new HttpsError("invalid-argument", "UID is not defined");
}

if (!user.emailVerified) {
logger.info("email is not verified.", user);
throw new HttpsError("failed-precondition", "email is not verified");
}

const userInfo = {
userId: uid,
username: `${slugify(user.displayName ?? "guest")}-${
Expand All @@ -53,10 +63,12 @@ export const beforecreated = beforeUserCreated(async (event) => {

await firestore.runTransaction(async (tx) => {
return tx.create(
firestore.collection("user_by_user_id").doc(uid),
firestore.collection(firebaseCollections.usersByUserId).doc(uid),
userInfo,
).create(
firestore.collection("user_id_by_username").doc(userInfo.username),
firestore.collection(firebaseCollections.userIdByUsername).doc(
userInfo.username,
),
{ userId: userInfo.userId },
);
}).catch((e) => {
Expand All @@ -66,3 +78,44 @@ export const beforecreated = beforeUserCreated(async (event) => {

logger.info("User creation complete");
});

export const updateUserRecords = beforeUserSignedIn(async (event) => {
const firestore = getFirestore();

const user = event.data;
if (!user) {
logger.info("User info is missing from request.", user);
throw new HttpsError("invalid-argument", "User info is missing");
}

const uid = user?.uid;
if (!uid) {
logger.info("uid is missing.", user);
throw new HttpsError("invalid-argument", "UID is not defined");
}

if (!user.emailVerified) {
logger.info("email is not verified.", user);
throw new HttpsError("failed-precondition", "email is not verified");
}

const userInfo = {
name: user.displayName,
email: user.email,
profilePictureUri: user.photoURL,
};

logger.info("Attempting to sign in user with info=", userInfo);

await firestore.runTransaction(async (tx) => {
return tx.update(
firestore.collection(firebaseCollections.usersByUserId).doc(uid),
userInfo,
);
}).catch((e) => {
logger.error("Failed to update user info records:", e);
throw new HttpsError("internal", "Failed to update user info records!");
});

logger.info("User sign in complete");
});
Loading

0 comments on commit 1504df7

Please sign in to comment.