@@ -17,6 +17,7 @@ import {
1717 watchWalletNetworkChanges ,
1818} from "@/lib/stellar" ;
1919import type { StellarNetwork } from "@/lib/network-config" ;
20+ import LegalConsentModal , { hasAcceptedLegal } from "@/components/LegalConsentModal" ;
2021import LegalConsentModal , { hasAcceptedLegal , acceptLegal } from "@/components/LegalConsentModal" ;
2122import ConfirmDialog from "@/components/ConfirmDialog" ;
2223import { CONFIRM_KEYS } from "@/lib/confirm-prefs" ;
@@ -25,6 +26,7 @@ import { checkConnectionRateLimit, recordConnectionSuccess, recordConnectionFail
2526
2627// Storage keys
2728const LAST_ACCOUNT_KEY = "stellarwork:last-connected-account" ;
29+ const WALLET_AUTO_RECONNECT_KEY = "stellarwork:wallet-auto-reconnect" ;
2830const JOB_CACHE_PREFIX = "job-desc:" ;
2931
3032interface WalletContextType {
@@ -95,6 +97,30 @@ function persistLastAccount(address: string | null) {
9597 }
9698}
9799
100+ function getLastConnectedAccount ( ) : string | null {
101+ if ( typeof window === "undefined" ) return null ;
102+ return localStorage . getItem ( LAST_ACCOUNT_KEY ) ;
103+ }
104+
105+ function getAutoReconnectPreference ( ) : boolean {
106+ if ( typeof window === "undefined" ) return false ;
107+ const stored = localStorage . getItem ( WALLET_AUTO_RECONNECT_KEY ) ;
108+ if ( stored === "true" ) return true ;
109+ if ( stored === "false" ) return false ;
110+ // Migrate existing sessions that stored an address before the flag existed.
111+ return getLastConnectedAccount ( ) !== null ;
112+ }
113+
114+ function setAutoReconnectPreference ( connected : boolean ) {
115+ if ( typeof window === "undefined" ) return ;
116+ localStorage . setItem ( WALLET_AUTO_RECONNECT_KEY , connected ? "true" : "false" ) ;
117+ }
118+
119+ function clearPersistedWalletSession ( ) {
120+ persistLastAccount ( null ) ;
121+ setAutoReconnectPreference ( false ) ;
122+ }
123+
98124export function WalletProvider ( { children } : { children : ReactNode } ) {
99125 const [ wallet , setWallet ] = useState < string | null > ( null ) ;
100126 const [ walletNetwork , setWalletNetwork ] = useState < StellarNetwork | null > ( null ) ;
@@ -107,14 +133,22 @@ export function WalletProvider({ children }: { children: ReactNode }) {
107133 setWalletNetwork ( nextNetwork ) ;
108134 } , [ ] ) ;
109135
110- // On mount: restore last session via Freighter if still allowed .
136+ // On mount: restore last session when the user opted in to auto-reconnect .
111137 useEffect ( ( ) => {
138+ if ( ! getAutoReconnectPreference ( ) ) {
139+ return ;
140+ }
141+
112142 getPublicKey ( ) . then ( async ( key ) => {
113143 if ( key ) {
114144 setWallet ( key ) ;
115145 persistLastAccount ( key ) ;
116146 await refreshWalletNetwork ( ) ;
147+ return ;
117148 }
149+
150+ // Freighter permission was revoked or the extension is unavailable.
151+ clearPersistedWalletSession ( ) ;
118152 } ) ;
119153 } , [ refreshWalletNetwork ] ) ;
120154
@@ -208,14 +242,15 @@ export function WalletProvider({ children }: { children: ReactNode }) {
208242 const key = await connectPromiseRef . current ;
209243 setWallet ( key ) ;
210244 persistLastAccount ( key ) ;
245+ setAutoReconnectPreference ( true ) ;
211246 await refreshWalletNetwork ( ) ;
212247 } , [ wallet , refreshWalletNetwork ] ) ;
213248
214249 const disconnectWallet = useCallback ( ( ) => {
215250 clearWalletData ( ) ;
216251 setWallet ( null ) ;
217252 setWalletNetwork ( null ) ;
218- persistLastAccount ( null ) ;
253+ clearPersistedWalletSession ( ) ;
219254 // Clear session display preference
220255 if ( typeof window !== "undefined" ) {
221256 sessionStorage . removeItem ( "wallet-display-mode" ) ;
@@ -242,6 +277,7 @@ export function WalletProvider({ children }: { children: ReactNode }) {
242277 clearWalletData ( ) ;
243278 setWallet ( newKey ) ;
244279 persistLastAccount ( newKey ) ;
280+ setAutoReconnectPreference ( true ) ;
245281 }
246282 await refreshWalletNetwork ( ) ;
247283 } catch ( err ) {
0 commit comments