Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Govt-Billing-React/src/firebase/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import {
signInWithEmailAndPassword,
signOut,
} from "firebase/auth";
import { auth } from "./index";
import { auth, isConfigured } from "./index";

const MOCK_USER = { uid: "local-dev-user", email: "dev@local.com", displayName: "Local Developer" };

const signUpWithEmailAndPassword = async (email: string, password: string) => {
if (!isConfigured) {
localStorage.setItem("mockUser", JSON.stringify({ ...MOCK_USER, email }));
window.location.reload();
return { ...MOCK_USER, email };
}
try {
const userCredential = await createUserWithEmailAndPassword(
auth,
Expand All @@ -23,6 +30,11 @@ const signUpWithEmailAndPassword = async (email: string, password: string) => {
};

const loginWithEmailPassword = async (email: string, password: string) => {
if (!isConfigured) {
localStorage.setItem("mockUser", JSON.stringify({ ...MOCK_USER, email }));
window.location.reload();
return { ...MOCK_USER, email };
}
try {
const userCredential = await signInWithEmailAndPassword(
auth,
Expand All @@ -40,6 +52,11 @@ const loginWithEmailPassword = async (email: string, password: string) => {
};

const logOut = async () => {
if (!isConfigured) {
localStorage.removeItem("mockUser");
window.location.reload();
return;
}
try {
await signOut(auth);
} catch (error) {
Expand Down
8 changes: 7 additions & 1 deletion Govt-Billing-React/src/firebase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ const firebaseConfig = {

let app: FirebaseApp, auth: Auth, db: Firestore;

const isConfigured = !!firebaseConfig.apiKey;

const initFirebase = () => {
if (!isConfigured) {
console.warn("Firebase is not configured. Running in local mock mode.");
return;
}
if (!app) {
app = initializeApp(firebaseConfig);
}
Expand All @@ -25,4 +31,4 @@ const initFirebase = () => {
}
};

export { initFirebase, app, auth, db };
export { initFirebase, app, auth, db, isConfigured };
14 changes: 11 additions & 3 deletions Govt-Billing-React/src/hooks/useUser.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useEffect, useState } from "react";
import { auth } from "../firebase";
import { auth, isConfigured } from "../firebase";
import { User } from "firebase/auth";

export default function useUser() {
const [user, setUser] = useState(null);
const [user, setUser] = useState<any>(null);
const [isLoading, setIsLoading] = useState(true);

const handleUser = (user: User) => {
const handleUser = (user: User | null) => {
if (user) {
setUser(user);
} else {
Expand All @@ -16,6 +16,14 @@ export default function useUser() {
setIsLoading(false);
};
useEffect(() => {
if (!isConfigured) {
const mockUserStr = localStorage.getItem("mockUser");
if (mockUserStr) {
setUser(JSON.parse(mockUserStr));
}
setIsLoading(false);
return;
}
const unsubscribe = auth.onIdTokenChanged(handleUser);
return () => unsubscribe();
}, []);
Expand Down