fix: add cross-tab session protection and improve auth redirects#55
Closed
NicoBernardes wants to merge 1 commit into
Closed
fix: add cross-tab session protection and improve auth redirects#55NicoBernardes wants to merge 1 commit into
NicoBernardes wants to merge 1 commit into
Conversation
- 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
approved these changes
Mar 11, 2026
Author
|
Superseded by #56 which includes these changes plus Casdoor SSO integration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
useAuthGuardhook uses thelocalStoragestorageevent 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.RequireAuthenticationnow redirects unauthenticated users to the login page with a flash message instead of returning a raw 401 error (which caused aflate: closed writererror on the error page).RequireNoAuthenticationnow 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.go—RequireAuthenticationredirects to login instead of 401;RequireNoAuthenticationadds a warning flash messageresources/js/hooks/useAuthGuard.ts— New hook with localStorage-based cross-tab detection + Inertia response safety netresources/js/Layouts/AppLayout.tsx— IntegrateduseAuthGuardresources/js/Layouts/AuthLayout.tsx— IntegrateduseAuthGuardresources/js/Layouts/PublicLayout.tsx— IntegrateduseAuthGuardTest plan
/user/register— should redirect to dashboard with warning toast/dashboardwhile not logged in — should redirect to/user/loginwith flash message (no 401 error)