Skip to content

Commit

Permalink
Develop
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfinol committed Nov 17, 2023
1 parent dc5bc43 commit 88cb2c2
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 527 deletions.
2 changes: 0 additions & 2 deletions app/upload/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Footer from "@/components/footer";
import Header from "@/components/header";
import { getAuthenticatedAppForUser } from "@/lib/firebase/firebase";
import { Metadata } from "next";
import Link from "next/link";

Expand All @@ -16,7 +15,6 @@ export const metadata: Metadata = {
};

export default async function Page() {
const { currentUser } = await getAuthenticatedAppForUser();
return (
<main className="main-container">
<Header title="CGS Games Upload" />
Expand Down
72 changes: 0 additions & 72 deletions components/upload.tsx

This file was deleted.

78 changes: 1 addition & 77 deletions lib/firebase/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getApps, initializeApp } from "firebase/app";
import { getAuth, signInWithCustomToken } from "firebase/auth";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";

Expand All @@ -17,79 +17,3 @@ export const firebaseApp =
export const auth = getAuth(firebaseApp);
export const db = getFirestore(firebaseApp);
export const storage = getStorage(firebaseApp);

export async function getAuthenticatedAppForUser(
session: string | undefined = "",
) {
if (typeof window !== "undefined") {
// client
console.log("client: ", firebaseApp);
return {
app: firebaseApp,
user: getAuth(firebaseApp).currentUser?.toJSON(),
};
}

const { initializeApp: initializeAdminApp, getApps: getAdminApps } =
await import("firebase-admin/app");
const { getAuth: getAdminAuth } = await import("firebase-admin/auth");
const { credential } = await import("firebase-admin");
const ADMIN_APP_NAME = "firebase-frameworks";
const adminApp =
getAdminApps().find((it: { name: string }) => it.name === ADMIN_APP_NAME) ||
initializeAdminApp(
{
credential: credential.applicationDefault(),
},
ADMIN_APP_NAME,
);
const adminAuth = getAdminAuth(adminApp);
const noSessionReturn = { app: null, currentUser: null };

if (!session) {
// if no session cookie was passed, try to get from next/headers for app router
session = await getAppRouterSession();
if (!session) return noSessionReturn;
}

const decodedIdToken = await adminAuth.verifySessionCookie(session);
const app = initializeAuthenticatedApp(decodedIdToken.uid);
const auth = getAuth(app);

// handle revoked tokens
const isRevoked = !(await adminAuth
.verifySessionCookie(session, true)
.catch((e: { message: any }) => console.error(e.message)));
if (isRevoked) return noSessionReturn;

// authenticate with custom token
if (auth.currentUser?.uid !== decodedIdToken.uid) {
const customToken = await adminAuth
.createCustomToken(decodedIdToken.uid)
.catch((e: { message: any }) => console.error(e.message));

if (!customToken) return noSessionReturn;

await signInWithCustomToken(auth, customToken);
}
console.log("server: ", app);
return { app, currentUser: auth.currentUser };
}

async function getAppRouterSession() {
// dynamically import to prevent import errors in pages router
const { cookies } = await import("next/headers");
try {
return cookies().get("__session")?.value;
} catch (error) {
// cookies() throws when called from pages router
return undefined;
}
}

function initializeAuthenticatedApp(uid: any) {
const random = Math.random().toString(36).split(".")[1];
const appName = `authenticated-context:${uid}:${random}`;
const app = initializeApp(firebaseConfig, appName);
return app;
}
Loading

0 comments on commit 88cb2c2

Please sign in to comment.