Skip to content

Commit 8d87151

Browse files
committed
Handle firebase not configured error for development
1 parent b408b80 commit 8d87151

File tree

6 files changed

+65
-16
lines changed

6 files changed

+65
-16
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,29 @@ You need `NodeJs` and `yarn` installed on your machine.
106106
npm install --global yarn
107107
```
108108

109+
### Firebase prerequisites (optional)
110+
111+
Firebase is used in this project for authentications and to store snippets. In order to contribute in the part requiring Firebase, create a file called `.env` inside the root folder and add the following credentials in it once you create a Firebase app.
112+
113+
```.env
114+
NEXT_PUBLIC_FIREBASE_API_KEY=<your_FIREBASE_APP_API_KEY>
115+
116+
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=<your_FIREBASE_APP_AUTH_DOMAIN>
117+
118+
NEXT_PUBLIC_FIREBASE_PROJECT_ID=<your_FIREBASE_APP_PROJECT_ID>
119+
120+
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=<your_FIREBASE_APP_STORAGE_BUCKET>
121+
122+
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=<your_FIREBASE_APP_MESSAGING_SENDER_ID>
123+
124+
NEXT_PUBLIC_FIREBASE_APP_ID=<your_FIREBASE_APP_APP_ID>
125+
126+
NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=<your_FIREBASE_APP_MEASUREMENT_ID>
127+
128+
```
129+
130+
It does not matter what credentials you add to your `.env` file, as the app won't crash while developing since the error is taken care of for the Firebase services that are unavailable.
131+
109132
### Installation
110133

111134
1. Clone the repo

components/editor/SnippngCodeArea.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const SnippngCodeArea = () => {
5050
} = editorConfig;
5151

5252
const saveSnippet = async () => {
53+
if (!db) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
54+
5355
if (!user) return;
5456
try {
5557
const docRef = await addDoc(
@@ -62,6 +64,8 @@ const SnippngCodeArea = () => {
6264
};
6365

6466
const updateSnippet = async () => {
67+
if (!db) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
68+
6569
if (!user || !uid) return;
6670
try {
6771
await updateDoc(doc(db, "user", user.uid, "snippets", uid), {

config/firebase.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import { initializeApp } from "firebase/app";
2-
import { getFirestore } from "firebase/firestore";
3-
import { getAuth } from "firebase/auth";
2+
import { Firestore, getFirestore } from "firebase/firestore";
3+
import { Auth, getAuth } from "firebase/auth";
4+
import { Analytics, getAnalytics } from "firebase/analytics";
45

5-
const firebaseConfig = {
6-
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
7-
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
8-
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
9-
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
10-
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
11-
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
12-
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
13-
};
6+
let auth: Auth | undefined;
7+
let db: Firestore | undefined;
8+
let analytics: Analytics | undefined;
149

15-
const app = initializeApp(firebaseConfig);
10+
try {
11+
const firebaseConfig = {
12+
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
13+
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
14+
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
15+
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
16+
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
17+
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
18+
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
19+
};
1620

17-
export const auth = getAuth(app);
18-
export const db = getFirestore(app);
21+
const app = initializeApp(firebaseConfig);
22+
23+
auth = getAuth(app);
24+
db = getFirestore(app);
25+
analytics = typeof window !== "undefined" ? getAnalytics(app) : undefined;
26+
} catch (error) {
27+
console.log(
28+
Error(
29+
"Error while setting up firebase. Please add .env file with required credentials for firebase setup"
30+
)
31+
);
32+
}
33+
34+
export { auth, db, analytics };

context/AuthContext.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@ const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
3030
const [user, setUser] = useState<User | null>(null);
3131

3232
const loginWithGithub = async () => {
33+
if (!auth) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
3334
await setPersistence(auth, browserLocalPersistence)
3435
.then(() => {
35-
return signInWithPopup(auth, provider);
36+
return signInWithPopup(auth!, provider);
3637
})
3738
.catch((error) => {
3839
alert(error.message);
3940
});
4041
};
4142

4243
const logout = async () => {
44+
if (!auth) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
4345
await signOut(auth);
4446
};
4547

4648
useEffect(() => {
49+
if (!auth) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
4750
const unsubscribe = onAuthStateChanged(auth, (fbUser) => {
4851
setUser(fbUser);
4952
});

pages/profile.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const UserProfile = () => {
2626
const router = useRouter();
2727

2828
const fetchCodeSnippets = async () => {
29+
if (!db) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
2930
if (!user) return;
3031
try {
3132
const snippets: SnippngEditorConfigInterface[] = [];
@@ -47,6 +48,7 @@ const UserProfile = () => {
4748
};
4849

4950
const deleteCodeSnippet = async (snippetId: string) => {
51+
if (!db) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
5052
if (!user || !snippetId) return;
5153
setDeletingSnippet(true);
5254
try {

pages/snippet/[uid].tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ const SavedSnippet = () => {
2121
const { uid } = router.query as { uid: string };
2222

2323
const fetchCodeSnippet = async () => {
24-
if (!user) {
24+
if (!user || !db) {
2525
setLoadingConfig(false);
26+
if (!db) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
2627
return;
2728
}
2829
try {

0 commit comments

Comments
 (0)