Skip to content

Commit f553ed1

Browse files
authored
fix: GitHub login - move Firebase config to env vars and fix popup/redirect flow
- src/lib/firebase.ts: read Firebase config from Docusaurus customFields, falling back to the original hardcoded values so existing local dev continues to work unchanged; remove unused getAnalytics import - docusaurus.config.ts: expose all Firebase config keys via customFields, each driven by a corresponding env var (FIREBASE_API_KEY, FIREBASE_AUTH_DOMAIN, FIREBASE_PROJECT_ID, etc.) with the original values as defaults - src/components/ui/FirebaseAuthGithub.tsx: call getRedirectResult on mount so the post-redirect OAuth result is captured; fall back to signInWithRedirect when signInWithPopup is blocked/closed; clear loading state only in onAuthStateChanged to avoid race condition - .env.example: document all Firebase environment variables with instructions Agent-Logs-Url: https://github.com/recodehive/recode-website/sessions/e3163eda-443f-4bc1-ba41-7ccbf2326699
1 parent efa2a63 commit f553ed1

5 files changed

Lines changed: 109 additions & 41 deletions

File tree

.env.example

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ GITHUB_TOKEN=your_github_token_here
1010
SHOPIFY_STORE_DOMAIN=your-store.myshopify.com
1111
SHOPIFY_STOREFRONT_ACCESS_TOKEN=your_storefront_access_token_here
1212

13-
# Firebase Configuration (if needed)
13+
# Firebase Configuration
14+
# Override these in production to use the correct Firebase project.
15+
# Find these values in Firebase Console > Project Settings > Your apps > SDK setup and configuration.
16+
# IMPORTANT: FIREBASE_AUTH_DOMAIN must match a domain authorized in
17+
# Firebase Console > Authentication > Settings > Authorized domains.
1418
# FIREBASE_API_KEY=your_firebase_api_key
15-
# FIREBASE_AUTH_DOMAIN=your_firebase_auth_domain
19+
# FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
20+
# FIREBASE_DATABASE_URL=https://your-project-default-rtdb.firebaseio.com
1621
# FIREBASE_PROJECT_ID=your_firebase_project_id
22+
# FIREBASE_STORAGE_BUCKET=your-project.firebasestorage.app
23+
# FIREBASE_MESSAGING_SENDER_ID=your_sender_id
24+
# FIREBASE_APP_ID=your_app_id
25+
# FIREBASE_MEASUREMENT_ID=your_measurement_id
1726

1827
# Analytics Configuration
1928
# GOOGLE_ANALYTICS_ID=your_google_analytics_id

docusaurus.config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,27 @@ const config: Config = {
276276
SHOPIFY_STOREFRONT_ACCESS_TOKEN:
277277
process.env.SHOPIFY_STOREFRONT_ACCESS_TOKEN ||
278278
"2503dfbf93132b42e627e7d53b3ba3e9",
279+
// Firebase configuration — override via environment variables in production
280+
FIREBASE_API_KEY:
281+
process.env.FIREBASE_API_KEY || "AIzaSyBSiO9d5tHuyyAeUCt37pxDWTT7jPSigaU",
282+
FIREBASE_AUTH_DOMAIN:
283+
process.env.FIREBASE_AUTH_DOMAIN ||
284+
"awesome-github-profiles.firebaseapp.com",
285+
FIREBASE_DATABASE_URL:
286+
process.env.FIREBASE_DATABASE_URL ||
287+
"https://awesome-github-profiles-default-rtdb.firebaseio.com",
288+
FIREBASE_PROJECT_ID:
289+
process.env.FIREBASE_PROJECT_ID || "awesome-github-profiles",
290+
FIREBASE_STORAGE_BUCKET:
291+
process.env.FIREBASE_STORAGE_BUCKET ||
292+
"awesome-github-profiles.firebasestorage.app",
293+
FIREBASE_MESSAGING_SENDER_ID:
294+
process.env.FIREBASE_MESSAGING_SENDER_ID || "490821849262",
295+
FIREBASE_APP_ID:
296+
process.env.FIREBASE_APP_ID ||
297+
"1:490821849262:web:7e97984d98f578b81f9d3f",
298+
FIREBASE_MEASUREMENT_ID:
299+
process.env.FIREBASE_MEASUREMENT_ID || "G-WM33JZYEV0",
279300
hooks: {
280301
onBrokenMarkdownLinks: "warn",
281302
},

src/components/ui/FirebaseAuthGithub.tsx

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import React, { useEffect, useState } from "react";
22
import { auth } from "../../lib/firebase";
3-
import { GithubAuthProvider, signInWithPopup, User } from "firebase/auth";
4-
5-
const uiConfig = {
6-
signInFlow: "popup",
7-
signInOptions: [
8-
{
9-
provider: GithubAuthProvider.PROVIDER_ID,
10-
// You can add scopes and custom parameters here if needed
11-
},
12-
],
13-
callbacks: {
14-
signInSuccessWithAuthResult: () => false, // Prevents redirect
15-
},
16-
};
3+
import {
4+
GithubAuthProvider,
5+
signInWithPopup,
6+
signInWithRedirect,
7+
getRedirectResult,
8+
User,
9+
} from "firebase/auth";
1710

1811
const FirebaseAuthGithub: React.FC = () => {
1912
const [user, setUser] = useState<User | null>(null);
20-
const [githubText, setGithubText] = useState("Sign in with GitHub"); // ✅ new state
13+
const [githubText, setGithubText] = useState("Sign in with GitHub");
14+
const [loading, setLoading] = useState(true);
2115

2216
useEffect(() => {
23-
const unregisterAuthObserver = auth.onAuthStateChanged((user) =>
24-
setUser(user as User),
25-
);
17+
// Handle redirect result (returned after signInWithRedirect flow).
18+
// onAuthStateChanged will fire afterwards and clear the loading state.
19+
getRedirectResult(auth).catch((error) => {
20+
console.error("GitHub redirect sign-in error:", error);
21+
});
22+
23+
// onAuthStateChanged fires once on mount (and after every auth change),
24+
// so it is the single place we clear the loading state.
25+
const unregisterAuthObserver = auth.onAuthStateChanged((firebaseUser) => {
26+
setUser(firebaseUser as User);
27+
setLoading(false);
28+
});
2629

27-
// ✅ new effect to change text on resize
2830
const handleResize = () => {
2931
if (window.innerWidth <= 1110) {
3032
setGithubText("Sign in");
@@ -33,7 +35,7 @@ const FirebaseAuthGithub: React.FC = () => {
3335
}
3436
};
3537

36-
handleResize(); // initial call
38+
handleResize();
3739
window.addEventListener("resize", handleResize);
3840

3941
return () => {
@@ -42,6 +44,36 @@ const FirebaseAuthGithub: React.FC = () => {
4244
};
4345
}, []);
4446

47+
const handleGithubSignIn = async () => {
48+
const provider = new GithubAuthProvider();
49+
try {
50+
// Try popup first; fall back to redirect if the popup is blocked
51+
await signInWithPopup(auth, provider);
52+
} catch (error: unknown) {
53+
const code =
54+
error && typeof error === "object" && "code" in error
55+
? (error as { code: string }).code
56+
: "";
57+
if (
58+
code === "auth/popup-blocked" ||
59+
code === "auth/popup-closed-by-user" ||
60+
code === "auth/cancelled-popup-request"
61+
) {
62+
try {
63+
await signInWithRedirect(auth, provider);
64+
} catch (redirectError) {
65+
console.error("GitHub redirect sign-in error:", redirectError);
66+
}
67+
} else {
68+
console.error("GitHub sign-in error:", error);
69+
}
70+
}
71+
};
72+
73+
if (loading) {
74+
return null;
75+
}
76+
4577
if (user) {
4678
return (
4779
<div
@@ -78,15 +110,6 @@ const FirebaseAuthGithub: React.FC = () => {
78110
);
79111
}
80112

81-
const handleGithubSignIn = async () => {
82-
const provider = new GithubAuthProvider();
83-
try {
84-
await signInWithPopup(auth, provider);
85-
} catch (error) {
86-
console.error("GitHub sign-in error:", error);
87-
}
88-
};
89-
90113
return (
91114
<div
92115
style={{ textAlign: "center" }}
@@ -105,7 +128,6 @@ const FirebaseAuthGithub: React.FC = () => {
105128
>
106129
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82a7.65 7.65 0 0 1 2-.27c.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
107130
</svg>
108-
{/* ✅ dynamic text */}
109131
<span className="github-text">{githubText}</span>
110132
</button>
111133
</div>

src/lib/firebase.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
import { initializeApp, getApps, getApp } from "firebase/app";
22
import { getAuth } from "firebase/auth";
3-
import { getAnalytics } from "firebase/analytics";
3+
import siteConfig from "@generated/docusaurus.config";
4+
5+
const fields = siteConfig.customFields || {};
46

57
const firebaseConfig = {
6-
apiKey: "AIzaSyBSiO9d5tHuyyAeUCt37pxDWTT7jPSigaU",
7-
authDomain: "awesome-github-profiles.firebaseapp.com",
8-
databaseURL: "https://awesome-github-profiles-default-rtdb.firebaseio.com",
9-
projectId: "awesome-github-profiles",
10-
storageBucket: "awesome-github-profiles.firebasestorage.app",
11-
messagingSenderId: "490821849262",
12-
appId: "1:490821849262:web:7e97984d98f578b81f9d3f",
13-
measurementId: "G-WM33JZYEV0",
8+
apiKey:
9+
(fields.FIREBASE_API_KEY as string) ||
10+
"AIzaSyBSiO9d5tHuyyAeUCt37pxDWTT7jPSigaU",
11+
authDomain:
12+
(fields.FIREBASE_AUTH_DOMAIN as string) ||
13+
"awesome-github-profiles.firebaseapp.com",
14+
databaseURL:
15+
(fields.FIREBASE_DATABASE_URL as string) ||
16+
"https://awesome-github-profiles-default-rtdb.firebaseio.com",
17+
projectId:
18+
(fields.FIREBASE_PROJECT_ID as string) || "awesome-github-profiles",
19+
storageBucket:
20+
(fields.FIREBASE_STORAGE_BUCKET as string) ||
21+
"awesome-github-profiles.firebasestorage.app",
22+
messagingSenderId:
23+
(fields.FIREBASE_MESSAGING_SENDER_ID as string) || "490821849262",
24+
appId:
25+
(fields.FIREBASE_APP_ID as string) ||
26+
"1:490821849262:web:7e97984d98f578b81f9d3f",
27+
measurementId:
28+
(fields.FIREBASE_MEASUREMENT_ID as string) || "G-WM33JZYEV0",
1429
};
1530

1631
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);

tsconfig.tsbuildinfo

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)