-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create private content route and data readers
- Loading branch information
1 parent
1fa831a
commit ffc1b61
Showing
21 changed files
with
357 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export const functionsRegion = "us-central1"; | ||
|
||
export const firebaseCollections = { | ||
usersByUserId: "users_by_userId", | ||
userIdByUsername: "userId_by_username", | ||
privateContent: "private_content", | ||
}; |
2 changes: 1 addition & 1 deletion
2
tools-nodejs/vighnesh153-astro/firebase-seed/auth_export/accounts.json
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 +1 @@ | ||
{"kind":"identitytoolkit#DownloadAccountResponse","users":[{"localId":"ekAHm4gvpTBjoSmvINUnt638btaM","createdAt":"1734187406426","lastLoginAt":"1734276869465","displayName":"Vighnesh Raut","photoUrl":"https://i.imgur.com/npqtdBu.png","providerUserInfo":[{"providerId":"google.com","rawId":"5762999240917570257694699278672391745578","federatedId":"5762999240917570257694699278672391745578","displayName":"Vighnesh Raut","photoUrl":"https://i.imgur.com/npqtdBu.png","email":"[email protected]","screenName":"vighnesh153"}],"validSince":"1734279393","email":"[email protected]","emailVerified":true,"disabled":false}]} | ||
{"kind":"identitytoolkit#DownloadAccountResponse","users":[{"localId":"ekAHm4gvpTBjoSmvINUnt638btaM","createdAt":"1734187406426","lastLoginAt":"1734882450193","displayName":"Vighnesh Raut","photoUrl":"https://i.imgur.com/npqtdBu.png","providerUserInfo":[{"providerId":"google.com","rawId":"5762999240917570257694699278672391745578","federatedId":"5762999240917570257694699278672391745578","displayName":"Vighnesh Raut","photoUrl":"https://i.imgur.com/npqtdBu.png","email":"[email protected]","screenName":"vighnesh153"}],"validSince":"1734881873","email":"[email protected]","emailVerified":true,"disabled":false,"lastRefreshAt":"2024-12-22T17:33:39.253Z"},{"localId":"iWtBCmoWRTXg6UTQCV7ncLJPxcNO","displayName":"Pikachu","photoUrl":"https://i.imgur.com/npqtdBu.png","screenName":"Pikachu","email":"[email protected]","emailVerified":true,"createdAt":"1734882413806","lastLoginAt":"1734882416118","providerUserInfo":[{"providerId":"google.com","rawId":"7759237076568892792428913761032108656749","federatedId":"7759237076568892792428913761032108656749","displayName":"Pikachu","photoUrl":"https://i.imgur.com/npqtdBu.png","email":"[email protected]","screenName":"Pikachu"}],"lastRefreshAt":"2024-12-22T15:46:56.118Z"}]} |
Binary file modified
BIN
+0 Bytes
(100%)
...e-seed/firestore_export/all_namespaces/all_kinds/all_namespaces_all_kinds.export_metadata
Binary file not shown.
Binary file modified
BIN
+893 Bytes
(240%)
...nodejs/vighnesh153-astro/firebase-seed/firestore_export/all_namespaces/all_kinds/output-0
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
...vighnesh153-astro/firebase-seed/firestore_export/firestore_export.overall_export_metadata
Binary file not shown.
37 changes: 37 additions & 0 deletions
37
tools-nodejs/vighnesh153-astro/functions/src/get_private_content.ts
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,37 @@ | ||
import { firestore } from "firebase-admin"; | ||
import * as logger from "firebase-functions/logger"; | ||
import { HttpsError, onCall } from "firebase-functions/v2/https"; | ||
|
||
import { firebaseCollections } from "../../constants"; | ||
import { hasPermission } from "../../permissions/mod"; | ||
|
||
const db = firestore(); | ||
|
||
export const getPrivateContent = onCall(async (req) => { | ||
const uid = req.auth?.uid; | ||
if (!uid) { | ||
logger.error("You are not logged in."); | ||
throw new HttpsError("unauthenticated", "Not logged in."); | ||
} | ||
|
||
if (!hasPermission(uid, "getPrivateContent")) { | ||
logger.error("You don't have permission to access this resource."); | ||
throw new HttpsError( | ||
"permission-denied", | ||
"You don't have permission to get private content.", | ||
); | ||
} | ||
|
||
try { | ||
const snapshot = await db.collection(firebaseCollections.privateContent) | ||
.get(); | ||
const docs = await snapshot.docs.map((doc) => doc.data()); | ||
return docs; | ||
} catch (e) { | ||
logger.error("Failed to fetch private content:", e); | ||
throw new HttpsError( | ||
"internal", | ||
"Some internal error occurred file fetching private content.", | ||
); | ||
} | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 +1,17 @@ | ||
{ | ||
"scripts": { | ||
"start": "npx firebase emulators:start --project demo-vighnesh153-app --import ./firebase-seed --export-on-exit ./firebase-seed", | ||
"start:functions-watch": "npm run --prefix functions build:watch", | ||
"start:emulators": "npx firebase emulators:start --project demo-vighnesh153-app --import ./firebase-seed --export-on-exit ./firebase-seed", | ||
"start": "npx conc --kill-others \"npm run start:functions-watch\" \"npm run start:emulators\"", | ||
"deploy": "npx firebase deploy --project vighnesh153-app", | ||
"deploy:hosting": "npx firebase deploy --only hosting --project vighnesh153-app", | ||
"deploy:functions": "npx firebase deploy --only functions --project vighnesh153-app" | ||
}, | ||
"dependencies": { | ||
"firebase-functions": "^6.1.1", | ||
"firebase-tools": "^13.28.0" | ||
}, | ||
"devDependencies": { | ||
"concurrently": "^9.1.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,17 @@ | ||
import { type Permissions } from "./permissions"; | ||
import { vighnesh153Permissions } from "./vighnesh153"; | ||
|
||
export function hasPermission( | ||
uid: string, | ||
permission: keyof Permissions, | ||
): boolean { | ||
return uidToPermissions[uid]?.[permission] ?? false; | ||
} | ||
|
||
const uidToPermissions: Record<string, Permissions> = { | ||
// prod | ||
"pIX2CHynXKYyobveA1B3Ym7T99Z2": vighnesh153Permissions, | ||
|
||
// local | ||
"ekAHm4gvpTBjoSmvINUnt638btaM": vighnesh153Permissions, | ||
}; |
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,4 @@ | ||
export interface Permissions { | ||
// IYKYK | ||
getPrivateContent?: boolean; | ||
} |
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,5 @@ | ||
import { type Permissions } from "./permissions"; | ||
|
||
export const vighnesh153Permissions: Permissions = { | ||
getPrivateContent: true, | ||
}; |
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
40 changes: 40 additions & 0 deletions
40
tools-nodejs/vighnesh153-astro/website/src/components/private/private_cards_collection.tsx
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,40 @@ | ||
import { createResource, For, type JSX, Show } from "solid-js"; | ||
import { useStore } from "@nanostores/solid"; | ||
|
||
import { classes } from "@/utils"; | ||
import { loggedInUserId } from "@/store/auth.ts"; | ||
import { getPrivateContent } from "@/store/private_content"; | ||
|
||
const fetchPrivateContent = (userId: string | null) => | ||
getPrivateContent(userId); | ||
|
||
export function PrivateCardsCollection(): JSX.Element { | ||
const $loggedInUserId = useStore(loggedInUserId); | ||
const [privateContent] = createResource($loggedInUserId, fetchPrivateContent); | ||
|
||
return ( | ||
<div> | ||
<Show when={privateContent.loading}> | ||
<p>Loading...</p> | ||
</Show> | ||
<Show when={privateContent.error || privateContent() === null}> | ||
<p>Failed to fetch content.</p> | ||
</Show> | ||
<Show when={privateContent() !== null}> | ||
<div | ||
class={classes(` | ||
grid gap-4 | ||
grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 | ||
`)} | ||
> | ||
<For each={privateContent()?.data ?? []}> | ||
{(card) => ( | ||
<div class="min-w-5 aspect-video bg-primary rounded-lg"></div> | ||
)} | ||
</For> | ||
</div> | ||
</Show> | ||
</div> | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
tools-nodejs/vighnesh153-astro/website/src/models/private_content.ts
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,12 @@ | ||
import { z } from "zod"; | ||
|
||
export const PrivateContent = z.object({ | ||
data: z.object({ | ||
id: z.string().min(1), | ||
localPath: z.string().min(1), | ||
imageUrl: z.string().min(1), | ||
videoUrl: z.string().min(1), | ||
}).array(), | ||
}); | ||
|
||
export type PrivateContent = z.infer<typeof PrivateContent>; |
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
Oops, something went wrong.