Crs-Lionel Claude Sonnet 4.6 commited on
Commit
beb4929
Β·
1 Parent(s): afa13e4

Wire up auth UI: Navbar login/signup/logout, re-enable tool guard

Browse files

- Add Sign In / Sign Up links + logged-in email/logout to Navbar
(signin/signup pages already existed but were unreachable)
- Re-enable auth guard on tool pages (redirect to /signin?next=)
- 401 handler now redirects anonymous actions to signin too, not
just expired-token cases (fixes silent "Upload failed")

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

frontend/src/app/tools/[toolId]/page.tsx CHANGED
@@ -11,7 +11,7 @@ import { useState, useRef, useCallback, use, useEffect, type Dispatch, type SetS
11
  import { useRouter, useSearchParams } from "next/navigation";
12
  import Link from "next/link";
13
  import { tools } from "@/lib/data";
14
- // import { useAuth } from "@/lib/AuthContext";
15
 
16
 
17
  function SvgIcon({ d, size = 16, stroke = "currentColor", strokeWidth = 1.6 }: {
@@ -821,14 +821,14 @@ export default function ToolPage({ params }: { params: Promise<{ toolId: string
821
  const cfg = CFGS[toolId];
822
  const tool = tools.find((t) => t.id === toolId);
823
 
824
- // ── Auth guard (commented out) ──────────────────────────────
825
- // const { isAuthenticated, isLoading: authLoading } = useAuth();
826
- //
827
- // useEffect(() => {
828
- // if (!authLoading && !isAuthenticated) {
829
- // router.push(`/signin?next=${encodeURIComponent(`/tools/${toolId}`)}`);
830
- // }
831
- // }, [authLoading, isAuthenticated, router, toolId]);
832
  // ────────────────────────────────────────────────────────────
833
 
834
  const [files, setFiles] = useState<FileEntry[]>([]);
 
11
  import { useRouter, useSearchParams } from "next/navigation";
12
  import Link from "next/link";
13
  import { tools } from "@/lib/data";
14
+ import { useAuth } from "@/lib/AuthContext";
15
 
16
 
17
  function SvgIcon({ d, size = 16, stroke = "currentColor", strokeWidth = 1.6 }: {
 
821
  const cfg = CFGS[toolId];
822
  const tool = tools.find((t) => t.id === toolId);
823
 
824
+ // ── Auth guard ──────────────────────────────────────────────
825
+ const { isAuthenticated, isLoading: authLoading } = useAuth();
826
+
827
+ useEffect(() => {
828
+ if (!authLoading && !isAuthenticated) {
829
+ router.push(`/signin?next=${encodeURIComponent(`/tools/${toolId}`)}`);
830
+ }
831
+ }, [authLoading, isAuthenticated, router, toolId]);
832
  // ────────────────────────────────────────────────────────────
833
 
834
  const [files, setFiles] = useState<FileEntry[]>([]);
frontend/src/components/Navbar.tsx CHANGED
@@ -2,6 +2,7 @@
2
  import { useEffect, useRef, useState } from "react";
3
  import Link from "next/link";
4
  import { useTheme } from "./ThemeProvider";
 
5
  import { tools } from "@/lib/data";
6
 
7
  function NavIcon({ d, size = 14 }: { d: string | string[]; size?: number }) {
@@ -17,6 +18,7 @@ export default function Navbar() {
17
  const [showMoreTools, setShowMoreTools] = useState(false);
18
  const toolsMenuRef = useRef<HTMLDivElement>(null);
19
  const { theme, toggle } = useTheme();
 
20
  const visibleTools = showMoreTools ? tools : tools.slice(0, 18);
21
 
22
  const closeMenu = () => {
@@ -113,7 +115,27 @@ export default function Navbar() {
113
  {theme === "dark" ? "Light" : "Dark"}
114
  </button>
115
 
116
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  </div>
118
  </div>
119
  </div>
 
2
  import { useEffect, useRef, useState } from "react";
3
  import Link from "next/link";
4
  import { useTheme } from "./ThemeProvider";
5
+ import { useAuth } from "@/lib/AuthContext";
6
  import { tools } from "@/lib/data";
7
 
8
  function NavIcon({ d, size = 14 }: { d: string | string[]; size?: number }) {
 
18
  const [showMoreTools, setShowMoreTools] = useState(false);
19
  const toolsMenuRef = useRef<HTMLDivElement>(null);
20
  const { theme, toggle } = useTheme();
21
+ const { isAuthenticated, user, logout, isLoading } = useAuth();
22
  const visibleTools = showMoreTools ? tools : tools.slice(0, 18);
23
 
24
  const closeMenu = () => {
 
115
  {theme === "dark" ? "Light" : "Dark"}
116
  </button>
117
 
118
+ {!isLoading && (
119
+ isAuthenticated ? (
120
+ <>
121
+ <span
122
+ className="nav-link"
123
+ style={{ maxWidth: 160, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", cursor: "default" }}
124
+ title={user?.email}
125
+ >
126
+ {user?.email}
127
+ </span>
128
+ <button suppressHydrationWarning className="nav-link" onClick={logout}>
129
+ Log out
130
+ </button>
131
+ </>
132
+ ) : (
133
+ <>
134
+ <Link href="/signin" className="nav-link">Sign In</Link>
135
+ <Link href="/signup" className="btn btn-dark">Sign Up</Link>
136
+ </>
137
+ )
138
+ )}
139
  </div>
140
  </div>
141
  </div>
frontend/src/lib/api.ts CHANGED
@@ -26,16 +26,17 @@ api.interceptors.response.use(
26
  error.response?.status === 401 &&
27
  typeof window !== "undefined"
28
  ) {
29
- const token = localStorage.getItem(TOKEN_KEY);
30
- if (token) {
31
- localStorage.removeItem(TOKEN_KEY);
32
- // Redirect to signin if on a protected page
33
- if (
34
- !window.location.pathname.startsWith("/signin") &&
35
- !window.location.pathname.startsWith("/signup")
36
- ) {
37
- window.location.href = "/signin";
38
- }
 
39
  }
40
  }
41
  return Promise.reject(error);
 
26
  error.response?.status === 401 &&
27
  typeof window !== "undefined"
28
  ) {
29
+ // Clear any stale token (no-op if none)
30
+ localStorage.removeItem(TOKEN_KEY);
31
+ // Any 401 (expired token OR anonymous action) β†’ send to signin,
32
+ // preserving where they were so they return after login.
33
+ const path = window.location.pathname;
34
+ if (
35
+ !path.startsWith("/signin") &&
36
+ !path.startsWith("/signup")
37
+ ) {
38
+ const next = encodeURIComponent(path + window.location.search);
39
+ window.location.href = `/signin?next=${next}`;
40
  }
41
  }
42
  return Promise.reject(error);