fix(auth): improve logout flow to always redirect to home

- Use window.location.href for full page refresh
- Move redirect to finally block to ensure it always executes
- Fix V7 migration to be a no-op since column is already jsonb
This commit is contained in:
wowo-zZ 2026-03-14 15:31:12 +08:00
parent 611253fe21
commit e97752735c
2 changed files with 12 additions and 13 deletions

View file

@ -1,12 +1,10 @@
-- Fix audit_log detail_json column to properly handle JSONB type
-- This migration ensures existing data is compatible with the JSONB type
-- The column is already defined as jsonb in V1, but we need to ensure
-- any existing string data can be properly cast to jsonb
ALTER TABLE audit_log
ALTER COLUMN detail_json TYPE jsonb
USING CASE
WHEN detail_json IS NULL THEN NULL
WHEN detail_json = '' THEN NULL
ELSE detail_json::jsonb
END;
-- The column is already defined as jsonb in V1, so this migration is a no-op
-- for fresh installations. For existing installations with text data, this would
-- have been needed, but since the column was always jsonb, we just verify it exists.
-- No-op migration: column is already jsonb in V1
-- This file exists to maintain migration version continuity
SELECT 1;

View file

@ -1,5 +1,5 @@
import { useTranslation } from 'react-i18next'
import { Link, useNavigate } from '@tanstack/react-router'
import { Link } from '@tanstack/react-router'
import { useQueryClient } from '@tanstack/react-query'
import { authApi } from '@/api/client'
import {
@ -22,7 +22,6 @@ interface UserMenuProps {
export function UserMenu({ user }: UserMenuProps) {
const { t } = useTranslation()
const navigate = useNavigate()
const queryClient = useQueryClient()
const hasRole = (role: string) => user.platformRoles?.includes(role) ?? false
@ -34,10 +33,12 @@ export function UserMenu({ user }: UserMenuProps) {
const handleLogout = async () => {
try {
await authApi.logout()
queryClient.setQueryData(['auth', 'me'], null)
navigate({ to: '/' })
} catch (error) {
console.error('Logout failed:', error)
} finally {
// Always clear cache and redirect, even if API call fails
queryClient.setQueryData(['auth', 'me'], null)
window.location.href = '/'
}
}