auth provider cahnged

Post_REVERT
ahmed.mujtaba 2026-09-11 01:19:04 +05:00
parent 38f45195dd
commit 0709efb016
1 changed files with 22 additions and 2 deletions

View File

@ -45,6 +45,23 @@ export default function AuthProvider({ children }) {
if (me.data) mergeUser(me.data) if (me.data) mergeUser(me.data)
}, [me.data]) }, [me.data])
// A failed /users/me means the stored session can no longer be trusted: the
// token is dead, the account was deactivated, or the case this exists for
// the record predates a role change and its cached identity and permissions
// are simply wrong. Purge it instead of leaving it on disk, because the
// optimistic paint below treats cached permissions as good enough to render
// the shell, so a stale record keeps showing the old role and the old nav on
// every load until someone clears storage by hand.
//
// Genuine expiry is already handled upstream (apiClient refreshes, and calls
// the session-expired handler when that fails); this catches everything else.
useEffect(() => {
if (!me.isError) return
clearSession()
qc.clear()
navigate('/auth/login?expired=1', { replace: true })
}, [me.isError, navigate, qc])
const signIn = useCallback( const signIn = useCallback(
async (email, password) => { async (email, password) => {
const res = await authApi.login(email, password) const res = await authApi.login(email, password)
@ -64,8 +81,11 @@ export default function AuthProvider({ children }) {
navigate('/auth/login', { replace: true }) navigate('/auth/login', { replace: true })
}, [navigate, qc]) }, [navigate, qc])
const user = me.data ?? session?.data ?? null // Once /users/me has failed the cached snapshot is not a fallback, it is the
const permissions = me.data?.permissions ?? session?.data?.permissions ?? null // thing that was wrong serving it would paint a stale name, role and nav.
const cachedUser = me.isError ? null : session?.data
const user = me.data ?? cachedUser ?? null
const permissions = me.data?.permissions ?? cachedUser?.permissions ?? null
const status = !session?.access_token const status = !session?.access_token
? 'anonymous' ? 'anonymous'