fix(auth): banner persists after email verification — add refreshUser() to flush sessionStorage cache
This commit is contained in:
parent
3f7f6fb621
commit
9659c58286
2 changed files with 23 additions and 3 deletions
|
|
@ -7,6 +7,7 @@ interface User {
|
|||
username: string;
|
||||
email: string;
|
||||
role: string;
|
||||
email_verified?: boolean;
|
||||
}
|
||||
|
||||
interface AuthContextType {
|
||||
|
|
@ -15,6 +16,7 @@ interface AuthContextType {
|
|||
isLoading: boolean;
|
||||
login: (username: string, password: string) => Promise<void>;
|
||||
logout: () => void;
|
||||
refreshUser: () => Promise<void>;
|
||||
isAuthenticated: boolean;
|
||||
}
|
||||
|
||||
|
|
@ -139,6 +141,22 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||
}
|
||||
};
|
||||
|
||||
const refreshUser = async () => {
|
||||
if (!token) return;
|
||||
const validationKey = `token_validated_${token.substring(0, 10)}`;
|
||||
sessionStorage.removeItem(validationKey);
|
||||
try {
|
||||
const response = await authApi.getProfile();
|
||||
if (response && 'data' in response) {
|
||||
setUser(response.data);
|
||||
localStorage.setItem('user', JSON.stringify(response.data));
|
||||
sessionStorage.setItem(validationKey, 'true');
|
||||
}
|
||||
} catch {
|
||||
// silently ignore — user stays logged in
|
||||
}
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
clearAuthData();
|
||||
toast.info('You have been logged out');
|
||||
|
|
@ -157,7 +175,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||
})();
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user, token, isLoading, login, logout, isAuthenticated }}>
|
||||
<AuthContext.Provider value={{ user, token, isLoading, login, logout, refreshUser, isAuthenticated }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,12 +5,13 @@ import { useAuth } from '@/contexts/AuthContext';
|
|||
import Logo from '@/components/brand/Logo';
|
||||
import axios from 'axios';
|
||||
|
||||
|
||||
type Status = 'verifying' | 'success' | 'error';
|
||||
|
||||
export default function VerifyEmail() {
|
||||
const [searchParams] = useSearchParams();
|
||||
const navigate = useNavigate();
|
||||
const { isAuthenticated } = useAuth();
|
||||
const { isAuthenticated, refreshUser } = useAuth();
|
||||
const [status, setStatus] = useState<Status>('verifying');
|
||||
const [message, setMessage] = useState('');
|
||||
|
||||
|
|
@ -22,9 +23,10 @@ export default function VerifyEmail() {
|
|||
return;
|
||||
}
|
||||
axios.post('/api/auth/verify-email', { token })
|
||||
.then(res => {
|
||||
.then(async res => {
|
||||
setStatus('success');
|
||||
setMessage(res.data.message || 'Email verified successfully!');
|
||||
await refreshUser();
|
||||
setTimeout(() => navigate('/dashboard'), 3000);
|
||||
})
|
||||
.catch(err => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue