1- import React , { useState } from "react" ;
2- import { Routes , Route , Navigate , Outlet } from "react-router-dom" ; // Import routing components
3- import AppsManager from "./components/AppsManager" ;
4- import LoginPage from "./pages/Login" ; // Import LoginPage
5- import SignupPage from "./pages/Signup" ; // Import SignupPage
6- import Logo from "./assets/logo.svg" ;
1+ import AppsManager from "@/components/AppsManager" ;
2+ import { ShieldIcon } from "lucide-react" ;
3+ import React from "react" ; // Removed useState
4+ import { Link , Navigate , Outlet , Route , Routes } from "react-router-dom" ; // Import routing components
75import { toast } from "sonner" ;
6+ import Logo from "./assets/logo.svg" ;
87import { MnemonicManager } from "./components/MnemonicManager" ;
9- import { ShieldIcon } from "lucide-react " ;
8+ import SparkleEffect from "./components/SparkleEffect " ;
109import { Button } from "./components/ui/button" ;
10+ import { useAuth } from "./context/AuthContext" ; // Import useAuth
11+ import LoginPage from "./pages/Login" ; // Import LoginPage
12+ import SignupPage from "./pages/Signup" ; // Import SignupPage
1113
1214// A component to protect routes that require authentication
13- const ProtectedRoute = ( {
14- token,
15- children,
16- } : {
17- token : string | null ;
18- children : React . ReactNode ;
19- } ) => {
15+ // Refactored ProtectedRoute to use AuthContext
16+ const ProtectedRoute = ( { children } : { children : React . ReactNode } ) => {
17+ const { token, isLoading } = useAuth ( ) ; // Use context
18+
19+ // Show loading indicator while checking auth status
20+ if ( isLoading ) {
21+ return < div > Loading authentication...</ div > ; // Or a proper spinner/skeleton
22+ }
23+
24+ // If not loading and no token, redirect to login
2025 if ( ! token ) {
2126 // Redirect them to the /login page, but save the current location they were
2227 // trying to go to when they were redirected. This allows us to send them
@@ -25,36 +30,38 @@ const ProtectedRoute = ({
2530 return < Navigate to = "/login" replace /> ;
2631 }
2732
28- return < > { children } </ > ; // Render children if token exists
33+ // If loading is finished and token exists, render the children
34+ return < > { children } </ > ;
2935} ;
3036
3137function App ( ) {
32- // Keep token state here to manage authentication globally
33- const [ token , setToken ] = useState < string | null > (
34- localStorage . getItem ( "authToken" )
35- ) ; // Initialize from localStorage
36-
37- const handleSetToken = ( newToken : string | null ) => {
38- setToken ( newToken ) ;
39- if ( newToken ) {
40- localStorage . setItem ( "authToken" , newToken ) ; // Store token in localStorage
41- } else {
42- localStorage . removeItem ( "authToken" ) ; // Remove token from localStorage on logout
43- }
44- } ;
45-
46- const [ showSecurity , setShowSecurity ] = React . useState ( false ) ;
47- const toggleSecurity = ( ) => setShowSecurity ( ( current ) => ! current ) ;
38+ // Use the Auth context - removed isLoading as it's handled in ProtectedRoute now
39+ const { token, logout } = useAuth ( ) ;
4840
41+ // Handle logout using context
4942 const handleLogout = ( ) => {
50- handleSetToken ( null ) ;
43+ logout ( ) ;
5144 toast ( "Logged out." ) ;
52- // No need to clear username/password state as it's managed in pages now
5345 } ;
5446
47+ // Optional: Show a loading state while the token is being loaded from localStorage
48+ // This prevents flashing the login page briefly for authenticated users.
49+ // However, the ProtectedRoute will also handle loading state.
50+ // if (isLoading) {
51+ // return <div>Loading...</div>; // Or a spinner component
52+ // }
53+
5554 return (
56- < div className = "flex flex-col items-center justify-center min-h-screen py-8" >
57- < img src = { Logo } className = "mb-5" alt = "Logo" /> { /* Added alt text */ }
55+ < div className = "font-sans flex flex-col items-center justify-center min-h-screen py-8" >
56+ < SparkleEffect count = { 70 } />
57+ < header className = "flex flex-col gap-3 items-center justify-center mb-10" >
58+ < Link to = "/" className = "z-10" >
59+ < img src = { Logo } alt = "Logo" />
60+ </ Link >
61+ < p className = "text-muted-foreground" >
62+ Simple web bitcoin wallet that connects to apps
63+ </ p >
64+ </ header >
5865 < Routes >
5966 { /* Public routes */ }
6067 < Route
@@ -63,7 +70,7 @@ function App() {
6370 token ? (
6471 < Navigate to = "/" />
6572 ) : (
66- < LoginPage setToken = { handleSetToken } />
73+ < LoginPage /> // Remove setToken prop
6774 )
6875 }
6976 />
@@ -73,7 +80,7 @@ function App() {
7380 token ? (
7481 < Navigate to = "/" />
7582 ) : (
76- < SignupPage setToken = { handleSetToken } />
83+ < SignupPage /> // Remove setToken prop
7784 )
7885 }
7986 />
@@ -82,46 +89,32 @@ function App() {
8289 < Route
8390 path = "/"
8491 element = {
85- < ProtectedRoute token = { token } >
86- { /* Outlet renders nested routes or the main content */ }
87- < Outlet />
88- </ ProtectedRoute >
89- }
90- >
91- { /* Default protected route content (e.g., dashboard) */ }
92- < Route
93- index
94- element = {
95- < div className = "w-full max-w-4xl px-4" >
96- { " " }
97- { /* Added container for content */ }
98- { /* Removed welcome message, AppsManager is the main content now */ }
99- { /* <p>
100- Token: <code>{token}</code>
101- </p> */ }
102- < div className = "flex justify-end mb-8 gap-4 -mt-14" >
103- < Button
104- variant = "outline"
105- onClick = { toggleSecurity } size = "icon" >
106- < ShieldIcon />
107- </ Button >
108- < Button
109- className = "backdrop-blur-xs"
110- variant = "outline"
111- onClick = { handleLogout } >
92+ < >
93+ < div className = "w-full max-w-screen-md" >
94+ < div className = "flex justify-end gap-4 -mt-29" >
95+ < Link to = "/security" >
96+ < Button variant = "outline" size = "icon" >
97+ < ShieldIcon />
98+ </ Button >
99+ </ Link >
100+ < Button variant = "outline" onClick = { handleLogout } >
112101 Logout
113102 </ Button >
114103 </ div >
115- { token && < AppsManager token = { token } /> }
116- { /* Render AppsManager only if token exists */ }
117- { token && showSecurity && < MnemonicManager token = { token } /> }
118104 </ div >
119- }
120- />
121- { /* Add other protected routes here if needed */ }
105+ < div className = "flex-1 w-full max-w-screen-md" >
106+ < ProtectedRoute >
107+ < Outlet />
108+ </ ProtectedRoute >
109+ </ div >
110+ </ >
111+ }
112+ >
113+ < Route index element = { < AppsManager /> } />
114+ < Route path = "/security" element = { < MnemonicManager /> } />
122115 </ Route >
123116
124- { /* Fallback for unknown routes */ }
117+ { /* Fallback for unknown routes - use token from context */ }
125118 < Route
126119 path = "*"
127120 element = { < Navigate to = { token ? "/" : "/login" } replace /> }
0 commit comments