feat: improve PWA auth robustness and session management
This commit is contained in:
20
public/sw.js
20
public/sw.js
@@ -33,12 +33,15 @@ self.addEventListener('fetch', (event) => {
|
|||||||
const url = new URL(event.request.url);
|
const url = new URL(event.request.url);
|
||||||
|
|
||||||
// CRITICAL: Always bypass cache for auth, api, and supabase requests
|
// CRITICAL: Always bypass cache for auth, api, and supabase requests
|
||||||
if (
|
const isAuthRequest = url.pathname.includes('/auth/') ||
|
||||||
url.pathname.includes('/auth/') ||
|
url.pathname.includes('/v1/auth/') ||
|
||||||
url.pathname.includes('/api/') ||
|
url.pathname.includes('/v1/token');
|
||||||
url.hostname.includes('supabase.co')
|
const isApiRequest = url.pathname.includes('/api/');
|
||||||
) {
|
const isSupabaseRequest = url.hostname.includes('supabase.co');
|
||||||
return; // Let it fall through to the network
|
|
||||||
|
if (isAuthRequest || isApiRequest || isSupabaseRequest) {
|
||||||
|
// console.log('[SW] Bypassing cache for:', url.pathname);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Network first for all other requests, especially navigation
|
// Network first for all other requests, especially navigation
|
||||||
@@ -58,7 +61,10 @@ self.addEventListener('fetch', (event) => {
|
|||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch((err) => {
|
||||||
|
if (event.request.mode === 'navigate') {
|
||||||
|
console.log('[SW] Navigation failed, attempting cache fallback for:', url.pathname);
|
||||||
|
}
|
||||||
return caches.match(event.request);
|
return caches.match(event.request);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -24,23 +24,28 @@ export default function Home() {
|
|||||||
// Check session
|
// Check session
|
||||||
const checkUser = async () => {
|
const checkUser = async () => {
|
||||||
try {
|
try {
|
||||||
|
// Proactively get session - this will trigger a refresh if needed
|
||||||
const { data: { session }, error } = await supabase.auth.getSession();
|
const { data: { session }, error } = await supabase.auth.getSession();
|
||||||
console.log('[Auth] Initial session check:', {
|
|
||||||
hasSession: !!session,
|
if (session) {
|
||||||
userId: session?.user?.id,
|
console.log('[Auth] Valid session found:', {
|
||||||
email: session?.user?.email,
|
userId: session.user.id,
|
||||||
error: error?.message
|
expiry: new Date(session.expires_at! * 1000).toLocaleString()
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
console.log('[Auth] No active session found.');
|
||||||
|
}
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error('[Auth] Session retrieval error:', error);
|
console.error('[Auth] Session check error:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
setUser(session?.user ?? null);
|
setUser(session?.user ?? null);
|
||||||
if (session?.user) {
|
if (session?.user) {
|
||||||
fetchCollection();
|
fetchCollection();
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[Auth] Fatal error checking user:', err);
|
console.error('[Auth] Unexpected error during session check:', err);
|
||||||
setUser(null);
|
setUser(null);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
|||||||
@@ -10,6 +10,21 @@ export default function PWARegistration() {
|
|||||||
.register('/sw.js')
|
.register('/sw.js')
|
||||||
.then((registration) => {
|
.then((registration) => {
|
||||||
console.log('SW registered: ', registration);
|
console.log('SW registered: ', registration);
|
||||||
|
|
||||||
|
// Check for updates
|
||||||
|
registration.onupdatefound = () => {
|
||||||
|
const installingWorker = registration.installing;
|
||||||
|
if (installingWorker == null) return;
|
||||||
|
installingWorker.onstatechange = () => {
|
||||||
|
if (installingWorker.state === 'installed') {
|
||||||
|
if (navigator.serviceWorker.controller) {
|
||||||
|
console.log('[SW] New content is available; please refresh.');
|
||||||
|
} else {
|
||||||
|
console.log('[SW] Content is cached for offline use.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
})
|
})
|
||||||
.catch((registrationError) => {
|
.catch((registrationError) => {
|
||||||
console.log('SW registration failed: ', registrationError);
|
console.log('SW registration failed: ', registrationError);
|
||||||
|
|||||||
Reference in New Issue
Block a user