Skip to content

Commit 1d21a0c

Browse files
committed
updated AddSnippetDialog & SnippetCard.tsx
1 parent 6ad6d4b commit 1d21a0c

5 files changed

Lines changed: 668 additions & 51 deletions

File tree

client/src/components/AddSnippetDialog.tsx

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,17 @@ export default function AddSnippetDialog({
9797

9898
// Determine if we're creating or updating
9999
const isEditing = isEditMode && snippetToEdit;
100-
const API_URL = window.location.origin + (isEditing
100+
const API_URL = isEditing
101101
? `/api/snippets/${snippetToEdit.id}`
102-
: '/api/snippets');
102+
: '/api/snippets';
103103

104-
const res = await fetch(API_URL, {
105-
method: isEditing ? 'PUT' : 'POST',
106-
headers: {
107-
"Content-Type": "application/json"
108-
},
109-
body: JSON.stringify(snippet),
110-
credentials: "include"
111-
});
112-
113-
if (!res.ok) {
114-
throw new Error(`HTTP error ${res.status}`);
115-
}
104+
// FIXED: Use apiRequest instead of direct fetch to include authentication
105+
const result = await apiRequest(
106+
isEditing ? 'PUT' : 'POST',
107+
API_URL,
108+
snippet
109+
);
116110

117-
const result = await res.json();
118111
console.log(isEditing ? "Snippet updated successfully:" : "Snippet created successfully:", result);
119112

120113
toast({
@@ -135,7 +128,7 @@ export default function AddSnippetDialog({
135128
console.error("Error creating snippet:", error);
136129
toast({
137130
title: "Error",
138-
description: "Failed to create snippet. Please try again.",
131+
description: `Failed to ${isEditMode ? 'update' : 'create'} snippet. Please try again.`,
139132
variant: "destructive",
140133
});
141134
} finally {
@@ -236,4 +229,4 @@ export default function AddSnippetDialog({
236229
</DialogContent>
237230
</Dialog>
238231
);
239-
}
232+
}

client/src/components/SnippetCard.tsx

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,13 @@ export default function SnippetCard({ snippet, viewMode }: SnippetCardProps) {
122122
}
123123
};
124124

125-
// Function to handle sharing
125+
// Function to handle sharing - FIXED: Now uses apiRequest
126126
const handleShare = async () => {
127127
try {
128128
setIsSharing(true);
129129

130-
// Direct fetch call with proper error handling
131-
const response = await fetch(`/api/snippets/${snippet.id}/share`, {
132-
method: 'POST',
133-
headers: {
134-
'Content-Type': 'application/json'
135-
}
136-
});
137-
138-
if (!response.ok) {
139-
throw new Error(`Error ${response.status}: ${response.statusText}`);
140-
}
141-
142-
const data = await response.json();
130+
// Use apiRequest instead of direct fetch to include authentication
131+
const data = await apiRequest('POST', `/api/snippets/${snippet.id}/share`);
143132

144133
if (data && data.shareId) {
145134
// Create the share URL and open the dialog
@@ -164,24 +153,13 @@ export default function SnippetCard({ snippet, viewMode }: SnippetCardProps) {
164153
}
165154
};
166155

167-
// Function to toggle public access
156+
// Function to toggle public access - FIXED: Now uses apiRequest
168157
const handleTogglePublic = async () => {
169158
try {
170159
setIsTogglePublic(true);
171160

172-
// Direct fetch call with proper error handling
173-
const response = await fetch(`/api/snippets/${snippet.id}/publish`, {
174-
method: 'POST',
175-
headers: {
176-
'Content-Type': 'application/json'
177-
}
178-
});
179-
180-
if (!response.ok) {
181-
throw new Error(`Error ${response.status}: ${response.statusText}`);
182-
}
183-
184-
const updatedSnippet = await response.json();
161+
// Use apiRequest instead of direct fetch to include authentication
162+
const updatedSnippet = await apiRequest('POST', `/api/snippets/${snippet.id}/publish`);
185163

186164
toast({
187165
title: updatedSnippet.isPublic ? "Snippet published" : "Snippet unpublished",

client/src/lib/firebase.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,31 @@ export const checkFirebaseAuth = async () => {
138138
return { user: null };
139139
};
140140

141-
// Add a global reference for debugging from the console
141+
// 9️⃣ Expose Firebase objects globally for console debugging
142142
if (typeof window !== 'undefined') {
143143
(window as any).__checkFirebaseAuth = checkFirebaseAuth;
144-
console.log("[Firebase] Added global debug function: __checkFirebaseAuth");
144+
(window as any).__firebaseApp = app;
145+
(window as any).__firebaseAuth = auth;
146+
(window as any).__googleProvider = googleProvider;
147+
148+
// Also expose auth state checker
149+
(window as any).__getCurrentUser = () => {
150+
return auth.currentUser;
151+
};
152+
153+
// And a simple auth status checker
154+
(window as any).__getAuthStatus = () => {
155+
const user = auth.currentUser;
156+
return {
157+
isSignedIn: !!user,
158+
user: user ? {
159+
uid: user.uid,
160+
email: user.email,
161+
displayName: user.displayName
162+
} : null,
163+
authReady: true
164+
};
165+
};
166+
167+
console.log("[Firebase] Added global debug functions: __checkFirebaseAuth, __firebaseApp, __firebaseAuth, __getCurrentUser, __getAuthStatus");
145168
}

server/routes.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ export const authMiddleware: RequestHandler = async (req, res, next) => {
4949

5050
/** ─── 3) Register all routes ─────────────────────────────────────────────── */
5151
export async function registerRoutes(app: Express): Promise<Server> {
52+
53+
// ─── 3.0) Health Check Endpoint ──────────────────────────────────
54+
app.get("/api/health", async (req: Request, res: Response) => {
55+
try {
56+
// Test database connection
57+
const client = await pool.connect();
58+
const dbResult = await client.query("SELECT NOW() as current_time");
59+
client.release();
60+
61+
res.json({
62+
status: "healthy",
63+
timestamp: new Date().toISOString(),
64+
database: "connected",
65+
dbTime: dbResult.rows[0].current_time,
66+
server: "running"
67+
});
68+
} catch (error: any) {
69+
console.error("Health check failed:", error);
70+
res.status(503).json({
71+
status: "unhealthy",
72+
timestamp: new Date().toISOString(),
73+
database: "disconnected",
74+
error: error.message,
75+
server: "running"
76+
});
77+
}
78+
});
79+
5280
// ─── 3.1) Firebase Auth endpoints ──────────────────────────────────
5381

5482
app.post("/api/auth/user", async (req: Request, res: Response) => {
@@ -1019,10 +1047,10 @@ export async function registerRoutes(app: Express): Promise<Server> {
10191047
}
10201048
});
10211049

1022-
// Catch-All 404 only for /api/* routes
1023-
app.use("/api", (req, res) => {
1024-
res.status(404).json({ message: "Not found" });
1025-
});
1050+
// Catch-All 404 only for /api/* routes
1051+
app.use("/api", (req, res) => {
1052+
res.status(404).json({ message: "Not found" });
1053+
});
10261054

10271055
// ────────────────────────────────────────────────────────────────
10281056
// ─── Finally, create and return the HTTP server ───────────────────

0 commit comments

Comments
 (0)