Skip to content

Commit 47b1bfb

Browse files
Merge pull request #6 from solid/feat/my-storages
Feat/my storages
2 parents ff1feb4 + de57a67 commit 47b1bfb

2 files changed

Lines changed: 79 additions & 18 deletions

File tree

app/components/AuthWrapper.tsx

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,46 @@ export default function AuthWrapper({ children }: AuthWrapperProps) {
2222
async function checkAuth() {
2323
try {
2424
setError(null);
25-
const redirectInfo = await handleIncomingRedirect();
25+
26+
// Always handle incoming redirect first - this is necessary to restore sessions
27+
// The library uses this to restore session state from localStorage
28+
// Set restorePreviousSession to true to enable session restoration on page refresh
29+
const redirectInfo = await handleIncomingRedirect({
30+
restorePreviousSession: true,
31+
});
32+
33+
// Get the session instance after handling redirect
2634
const session = getDefaultSession();
2735

28-
// Log authentication response after redirect
29-
console.log("=== Authentication Response ===");
36+
// development logs (will remove later)
37+
console.log("=== Session Check ===");
3038
console.log("Redirect Info:", redirectInfo);
3139
console.log("Session Info:", session.info);
32-
console.log("WebID:", session.info.webId);
3340
console.log("Is Logged In:", session.info.isLoggedIn);
41+
console.log("WebID:", session.info.webId);
3442
console.log("Session ID:", session.info.sessionId);
35-
console.log("===============================");
43+
if (session.info.expirationDate) {
44+
const expDate = new Date(session.info.expirationDate);
45+
console.log("Expiration Date:", expDate.toISOString());
46+
console.log("Is Expired:", expDate <= new Date());
47+
}
3648

37-
setIsAuthenticated(session.info.isLoggedIn);
49+
// Check if we have a valid session
50+
let isLoggedIn = session.info.isLoggedIn && !!session.info.webId;
51+
52+
// Check expiration if session exists
53+
if (isLoggedIn && session.info.expirationDate) {
54+
const expirationDate = new Date(session.info.expirationDate);
55+
const now = new Date();
56+
if (expirationDate <= now) {
57+
console.log("Session expired, user needs to re-login");
58+
isLoggedIn = false;
59+
}
60+
}
61+
62+
63+
64+
setIsAuthenticated(isLoggedIn);
3865
} catch (err) {
3966
console.error("Auth check failed:", err);
4067
const errorMessage =
@@ -51,11 +78,13 @@ export default function AuthWrapper({ children }: AuthWrapperProps) {
5178

5279
// Re-check authentication state periodically in case user logs in from another tab
5380
useEffect(() => {
54-
if (!isAuthenticated && !error) {
55-
const interval = setInterval(async () => {
56-
try {
57-
const redirectInfo = await handleIncomingRedirect();
58-
const session = getDefaultSession();
81+
if (!isAuthenticated && !error) {
82+
const interval = setInterval(async () => {
83+
try {
84+
const redirectInfo = await handleIncomingRedirect({
85+
restorePreviousSession: true,
86+
});
87+
const session = getDefaultSession();
5988
if (session.info.isLoggedIn) {
6089
// Log authentication response after redirect (from polling)
6190
console.log("=== Authentication Response (from polling) ===");
@@ -83,7 +112,7 @@ export default function AuthWrapper({ children }: AuthWrapperProps) {
83112
setIsChecking(true);
84113
setIsAuthenticated(null);
85114
// Trigger re-check
86-
handleIncomingRedirect()
115+
handleIncomingRedirect({ restorePreviousSession: true })
87116
.then(() => {
88117
const session = getDefaultSession();
89118
setIsAuthenticated(session.info.isLoggedIn);

app/lib/hooks/useSolidStorages.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,13 @@ export function useSolidStorages(): UseSolidStoragesResult {
155155
const [error, setError] = useState<Error | null>(null);
156156

157157
useEffect(() => {
158+
let isMounted = true;
159+
let checkInterval: NodeJS.Timeout | null = null;
160+
158161
async function fetchStorages() {
159162
try {
163+
if (!isMounted) return;
164+
160165
setIsLoading(true);
161166
setError(null);
162167

@@ -171,19 +176,37 @@ export function useSolidStorages(): UseSolidStoragesResult {
171176
console.log("=========================");
172177

173178
// Set up a polling mechanism to check when authentication completes
174-
const checkInterval = setInterval(() => {
179+
// Keep isLoading as true while waiting for authentication
180+
checkInterval = setInterval(() => {
181+
if (!isMounted) {
182+
if (checkInterval) clearInterval(checkInterval);
183+
return;
184+
}
185+
175186
const currentSession = getDefaultSession();
176187
if (currentSession.info.isLoggedIn && currentSession.info.webId) {
177-
clearInterval(checkInterval);
178-
// Trigger re-fetch by updating state
188+
if (checkInterval) clearInterval(checkInterval);
189+
// Trigger re-fetch by calling fetchStorages again
179190
fetchStorages();
180191
}
181192
}, 500);
182193

183194
// Clear interval after 10 seconds to avoid infinite polling
184-
setTimeout(() => clearInterval(checkInterval), 10000);
195+
setTimeout(() => {
196+
if (checkInterval) {
197+
clearInterval(checkInterval);
198+
checkInterval = null;
199+
}
200+
// Only set loading to false if we've given up waiting and component is still mounted
201+
if (isMounted) {
202+
const finalSession = getDefaultSession();
203+
if (!finalSession.info.isLoggedIn || !finalSession.info.webId) {
204+
setIsLoading(false);
205+
}
206+
}
207+
}, 10000);
185208

186-
setIsLoading(false);
209+
// Don't set isLoading to false here - keep it true while waiting
187210
return;
188211
}
189212

@@ -581,14 +604,23 @@ export function useSolidStorages(): UseSolidStoragesResult {
581604

582605
// Also set up a listener for session changes
583606
const checkSession = setInterval(() => {
607+
if (!isMounted) {
608+
clearInterval(checkSession);
609+
return;
610+
}
611+
584612
const session = getDefaultSession();
585613
if (session.info.isLoggedIn && session.info.webId && storages.length === 0 && !isLoading) {
586614
console.log("Session state changed, re-fetching storages...");
587615
fetchStorages();
588616
}
589617
}, 1000);
590618

591-
return () => clearInterval(checkSession);
619+
return () => {
620+
isMounted = false;
621+
if (checkInterval) clearInterval(checkInterval);
622+
clearInterval(checkSession);
623+
};
592624
}, []);
593625

594626
return { storages, isLoading, error };

0 commit comments

Comments
 (0)