Skip to content

Commit

Permalink
refactor firebase providers concealing firebaseConfig in .env
Browse files Browse the repository at this point in the history
  • Loading branch information
jackavh committed May 15, 2024
1 parent 3358c78 commit 605e369
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 93 deletions.
15 changes: 14 additions & 1 deletion frontend/src/app/admin/firebase/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FirebaseApp, FirebaseOptions, initializeApp } from "firebase/app";
import { Auth, getAuth, signInWithEmailAndPassword, signOut } from "firebase/auth";
import { FirebaseStorage, getStorage } from "firebase/storage";

import env from "../util/validateEnv";

Expand All @@ -16,8 +17,9 @@ export const initFirebase = () => {

const app: FirebaseApp = initializeApp(firebaseConfig);
const auth: Auth = getAuth(app);
const storage: FirebaseStorage = getStorage(app);

return auth;
return { app, auth, storage };
};

/**
Expand Down Expand Up @@ -47,3 +49,14 @@ export const firebaseSignOut = async (auth: Auth) => {
alert(error);
}
};

/**
* Get's the firebase config for use with reactfire components
*
*/
export const getFirebaseConfig = (): FirebaseOptions => {
if (!env.NEXT_PUBLIC_FIREBASE_SETTINGS) {
throw new Error("Cannot get firebase settings");
}
return env.NEXT_PUBLIC_FIREBASE_SETTINGS as FirebaseOptions;
};
26 changes: 0 additions & 26 deletions frontend/src/app/admin/firebase/firebaseContext.tsx

This file was deleted.

26 changes: 26 additions & 0 deletions frontend/src/app/admin/firebase/firebaseProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use client";

import { Auth } from "firebase/auth";
import React, { createContext, useContext, useMemo } from "react";

import { getFirebaseConfig, initFirebase } from "./firebase";
import FirebaseProviderSDKs from "./reactfireProvider";

const FirebaseContext = createContext<Auth | null>(null);

/**
* Firebase Context Provider giving access to Auth object in components
*/
export const FirebaseProvider = ({ children }: { children: React.ReactNode }) => {
const { auth, storage } = useMemo(() => initFirebase(), []);

return (
<FirebaseContext.Provider value={auth}>
<FirebaseProviderSDKs storage={storage} firebaseConfig={getFirebaseConfig()}>
{children}
</FirebaseProviderSDKs>
</FirebaseContext.Provider>
);
};

export const useFirebase = () => useContext(FirebaseContext);
26 changes: 26 additions & 0 deletions frontend/src/app/admin/firebase/reactfireProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use client";

import { FirebaseOptions } from "firebase/app";
import { FirebaseStorage } from "firebase/storage";
import { FC, ReactNode } from "react";
import { FirebaseAppProvider, StorageProvider } from "reactfire";

type FirebaseProviderSDKsProps = {
storage: FirebaseStorage;
firebaseConfig: FirebaseOptions;
children: ReactNode;
};

const FirebaseProviderSDKs: FC<FirebaseProviderSDKsProps> = ({
storage,
firebaseConfig,
children,
}: FirebaseProviderSDKsProps) => {
return (
<FirebaseAppProvider firebaseConfig={firebaseConfig}>
<StorageProvider sdk={storage}>{children}</StorageProvider>
</FirebaseAppProvider>
);
};

export default FirebaseProviderSDKs;
53 changes: 0 additions & 53 deletions frontend/src/app/admin/util/firebase-providers.tsx

This file was deleted.

8 changes: 0 additions & 8 deletions frontend/src/app/admin/util/firebaseconfig.ts

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/src/app/admin/util/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRouter } from "next/navigation";
import { useEffect } from "react";

import { useFirebase } from "../firebase/firebaseContext";
import { useFirebase } from "../firebase/firebaseProvider";

/**
* Hook that returns the state of login for the user
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inter, Open_Sans, Roboto_Slab } from "next/font/google";

import { FirebaseProvider } from "./admin/firebase/firebaseContext";
import { FirebaseProvider } from "./admin/firebase/firebaseProvider";

import type { Metadata } from "next";

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/UserIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useRouter } from "next/navigation";
import * as React from "react";

import { firebaseSignOut } from "@/app/admin/firebase/firebase";
import { useFirebase } from "@/app/admin/firebase/firebaseContext";
import { useFirebase } from "@/app/admin/firebase/firebaseProvider";

export default function UserIcon() {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/admin/ForgotPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { sendPasswordResetEmail } from "firebase/auth";
import React, { Dispatch, SetStateAction, useState } from "react";

import { useFirebase } from "@/app/admin/firebase/firebaseContext";
import { useFirebase } from "@/app/admin/firebase/firebaseProvider";

type ForgotPasswordProps = {
setForgotPass: Dispatch<SetStateAction<boolean>>;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/admin/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRouter } from "next/navigation";
import React, { Dispatch, SetStateAction, useState } from "react";

import { firebaseSignIn } from "@/app/admin/firebase/firebase";
import { useFirebase } from "@/app/admin/firebase/firebaseContext";
import { useFirebase } from "@/app/admin/firebase/firebaseProvider";

import styles from "./LoginForm.module.css";

Expand Down

0 comments on commit 605e369

Please sign in to comment.