Skip to content

Commit 0d91934

Browse files
authored
Merge pull request #3 from hexawulf/public-view-system
fix: Resolve post-login 404 and PublicHome button functionality
2 parents 3e66d91 + 5e74e99 commit 0d91934

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

client/src/App.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// client/src/App.tsx
22

33
import React, { useEffect, useState } from "react";
4-
import { Switch, Route } from "wouter";
4+
import { Redirect, Switch, Route } from "wouter"; // Added Redirect
55
import { TooltipProvider } from "@/components/ui/tooltip";
66
import { ThemeProvider } from "@/contexts/ThemeContext";
77
import { CodeThemeProvider } from "@/contexts/CodeThemeContext";
@@ -29,6 +29,7 @@ function AuthenticatedRouter() {
2929
<Route path="/tags" component={Tags} />
3030
<Route path="/settings" component={Settings} />
3131
<Route path="/shared/:shareId" component={SharedSnippet} />
32+
<Route path="/login"><Redirect to="/" /></Route> {/* Added for authenticated users */}
3233
<Route component={NotFound} />
3334
</Switch>
3435
);
@@ -38,13 +39,47 @@ function PublicRouter() {
3839
return (
3940
<Switch>
4041
<Route path="/" component={PublicHome} />
42+
<Route path="/login" component={SignInTriggerPage} /> {/* Added for unauthenticated users */}
4143
<Route path="/shared/:shareId" component={SharedSnippet} />
4244
{/* For non-matched routes, redirect to PublicHome */}
4345
<Route component={PublicHome} />
4446
</Switch>
4547
);
4648
}
4749

50+
// SignInTriggerPage Helper Component
51+
const SignInTriggerPage: React.FC = () => {
52+
const { signIn, user, loading } = useAuthContext(); // useAuthContext is already imported
53+
useEffect(() => {
54+
// Only attempt to sign in if not already authenticated and not loading
55+
if (!user && !loading) {
56+
signIn();
57+
}
58+
}, [signIn, user, loading]);
59+
60+
// If already logged in (e.g., due to fast auth resolution), redirect to home.
61+
if (user) {
62+
return <Redirect to="/" />;
63+
}
64+
// If still loading auth state
65+
if (loading) {
66+
return (
67+
<div className="h-screen flex flex-col items-center justify-center">
68+
<div className="mb-4">Loading authentication...</div>
69+
<div className="animate-spin h-8 w-8 border-t-2 border-b-2 border-blue-500 rounded-full"></div>
70+
</div>
71+
);
72+
}
73+
// Default state while sign-in is being triggered or if user backs out of Google prompt
74+
return (
75+
<div className="h-screen flex flex-col items-center justify-center">
76+
<p>Redirecting to sign-in...</p>
77+
{/* Or redirect to PublicHome if sign-in is not immediate / user needs to click again */}
78+
{/* For now, this message is fine as signIn() should overlay Google prompt */}
79+
</div>
80+
);
81+
};
82+
4883
// Added debug component to show authentication state
4984
function AuthDebug({ user, loading }: { user: any, loading: boolean }) {
5085
return (
@@ -135,4 +170,4 @@ export default function App() {
135170
</CodeThemeProvider>
136171
</ThemeProvider>
137172
);
138-
}
173+
}

client/src/pages/PublicHome.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// client/src/pages/PublicHome.tsx
22
import React, { useState, useEffect, useMemo } from 'react';
3-
import { Link } from 'wouter'; // Or your routing library
3+
import { Link } from 'wouter';
4+
import { useAuthContext } from '@/contexts/AuthContext'; // Added
45
import SnippetGrid from '@/components/SnippetGrid'; // Adjust path as needed
56
import { Input } from '@/components/ui/input'; // Adjust path for your UI components
67
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; // Adjust path
@@ -11,6 +12,7 @@ import Layout from '@/components/Layout'; // Adjust path for Layout component
1112
const ALL_ITEMS_VALUE = "_ALL_"; // Define placeholder for "All" options
1213

1314
const PublicHome: React.FC = () => {
15+
const { signIn } = useAuthContext(); // Added
1416
const [snippets, setSnippets] = useState<Snippet[]>([]);
1517
const [allSnippets, setAllSnippets] = useState<Snippet[]>([]); // Store all fetched snippets for client-side filtering
1618
const [isLoading, setIsLoading] = useState<boolean>(true);
@@ -119,11 +121,13 @@ const PublicHome: React.FC = () => {
119121
<p className="text-xl text-gray-600 dark:text-gray-300 mb-6">
120122
Discover & Share Code Snippets with the World.
121123
</p>
122-
<Link href="/login"> {/* Adjust if your login route is different */}
123-
<Button size="lg" className="bg-blue-600 hover:bg-blue-700 text-white">
124-
Sign In / Sign Up
125-
</Button>
126-
</Link>
124+
<Button
125+
size="lg"
126+
className="bg-blue-600 hover:bg-blue-700 text-white"
127+
onClick={() => signIn()} // Call the signIn function from context
128+
>
129+
Sign In / Sign Up
130+
</Button>
127131
</header>
128132

129133
<div className="mb-8 p-6 bg-white dark:bg-gray-800 shadow-lg rounded-lg">
@@ -208,4 +212,4 @@ const PublicHome: React.FC = () => {
208212
);
209213
};
210214

211-
export default PublicHome;
215+
export default PublicHome;

0 commit comments

Comments
 (0)