Skip to content

Commit 5ac6415

Browse files
Updated the authentication check to restore sessions on page refresh (if session still valid)
1 parent 8f79499 commit 5ac6415

1 file changed

Lines changed: 41 additions & 12 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);

0 commit comments

Comments
 (0)