Skip to content

fix: add cross-tab session protection and improve auth redirects#55

Closed
NicoBernardes wants to merge 1 commit into
occult:mainfrom
NicoBernardes:fix/cross-tab-session-protection
Closed

fix: add cross-tab session protection and improve auth redirects#55
NicoBernardes wants to merge 1 commit into
occult:mainfrom
NicoBernardes:fix/cross-tab-session-protection

Conversation

@NicoBernardes

Copy link
Copy Markdown

Summary

  • Cross-tab session guard: New useAuthGuard hook uses the localStorage storage event API to detect in real-time when another tab logs in as a different user or logs out, and redirects accordingly with a toast notification.
  • Improved auth redirects: RequireAuthentication now redirects unauthenticated users to the login page with a flash message instead of returning a raw 401 error (which caused a flate: closed writer error on the error page).
  • Login/register protection: RequireNoAuthentication now shows a warning message ("You are already logged in as X. Please log out first to use another account.") before redirecting authenticated users away from login/register pages.

Problem

When a user is logged in on one browser tab and registers/logs in as a different user on another tab, the shared session cookie silently switches accounts. The first tab continues showing the old user's UI while the session belongs to the new user, which can lead to actions performed on the wrong account.

Changes

  • pkg/middleware/auth.goRequireAuthentication redirects to login instead of 401; RequireNoAuthentication adds a warning flash message
  • resources/js/hooks/useAuthGuard.ts — New hook with localStorage-based cross-tab detection + Inertia response safety net
  • resources/js/Layouts/AppLayout.tsx — Integrated useAuthGuard
  • resources/js/Layouts/AuthLayout.tsx — Integrated useAuthGuard
  • resources/js/Layouts/PublicLayout.tsx — Integrated useAuthGuard

Test plan

  • Log in as User A on Tab 1, open Tab 2 and try to access /user/register — should redirect to dashboard with warning toast
  • Log in as User A on Tab 1, log out on Tab 2 — Tab 1 should show toast and redirect to login
  • Log in as User A on Tab 1, log in as User B on Tab 2 — Tab 1 should show toast and redirect to home
  • Access /dashboard while not logged in — should redirect to /user/login with flash message (no 401 error)

- Add useAuthGuard hook with localStorage storage event for real-time
  cross-tab session change detection
- Redirect unauthenticated users to login instead of returning 401
- Show warning message when authenticated users try to access login/register
@felipekafuri felipekafuri marked this pull request as draft March 11, 2026 23:40
@NicoBernardes

Copy link
Copy Markdown
Author

Superseded by #56 which includes these changes plus Casdoor SSO integration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants