/** * Example: Cách sử dụng useAuthStore trong components * * File này chỉ để tham khảo, không được sử dụng * trong production */ import { useNavigate } from 'react-router-dom'; import useAuthStore from '../store/useAuthStore'; // ============================================ // Example 1: Login Component // ============================================ export const LoginExample = () => { const { login, isLoading, error } = useAuthStore(); const navigate = useNavigate(); const handleLogin = async ( email: string, password: string ) => { try { await login({ email, password }); navigate('/dashboard'); } catch (error) { // Error đã được xử lý trong store console.error('Login failed:', error); } }; return (
{error &&

{error}

}
); }; // ============================================ // Example 2: Register Component // ============================================ export const RegisterExample = () => { const { register, isLoading } = useAuthStore(); const navigate = useNavigate(); const handleRegister = async () => { try { await register({ name: 'John Doe', email: 'john@example.com', password: 'password123', phone: '0123456789' }); navigate('/login'); } catch (error) { console.error('Register failed:', error); } }; return ( ); }; // ============================================ // Example 3: User Profile Display // ============================================ export const UserProfileExample = () => { const { userInfo, isAuthenticated } = useAuthStore(); if (!isAuthenticated) { return

Vui lòng đăng nhập

; } return (

Thông tin người dùng

Tên: {userInfo?.name}

Email: {userInfo?.email}

Role: {userInfo?.role}

{userInfo?.avatar && ( {userInfo.name} )}
); }; // ============================================ // Example 4: Logout Button // ============================================ export const LogoutButtonExample = () => { const { logout, isLoading } = useAuthStore(); const navigate = useNavigate(); const handleLogout = async () => { await logout(); navigate('/login'); }; return ( ); }; // ============================================ // Example 5: Forgot Password // ============================================ export const ForgotPasswordExample = () => { const { forgotPassword, isLoading } = useAuthStore(); const handleForgotPassword = async ( email: string ) => { try { await forgotPassword({ email }); // Toast sẽ hiển thị thông báo thành công } catch (error) { console.error('Forgot password failed:', error); } }; return ( ); }; // ============================================ // Example 6: Reset Password // ============================================ export const ResetPasswordExample = () => { const { resetPassword, isLoading } = useAuthStore(); const navigate = useNavigate(); const handleResetPassword = async ( token: string, password: string ) => { try { await resetPassword({ token, password, confirmPassword: password }); navigate('/login'); } catch (error) { console.error('Reset password failed:', error); } }; return ( ); }; // ============================================ // Example 7: Conditional Rendering by Role // ============================================ export const RoleBasedComponentExample = () => { const { userInfo } = useAuthStore(); return (
{userInfo?.role === 'admin' && ( )} {userInfo?.role === 'staff' && ( )} {userInfo?.role === 'customer' && ( )}
); }; // ============================================ // Example 8: Auth State Check // ============================================ export const AuthStateCheckExample = () => { const { isAuthenticated, isLoading, token } = useAuthStore(); if (isLoading) { return

Đang tải...

; } if (!isAuthenticated || !token) { return

Bạn chưa đăng nhập

; } return

Bạn đã đăng nhập

; }; // ============================================ // Example 9: Update User Info // ============================================ export const UpdateUserInfoExample = () => { const { userInfo, setUser } = useAuthStore(); const handleUpdateProfile = () => { if (userInfo) { setUser({ ...userInfo, name: 'New Name', avatar: 'https://example.com/avatar.jpg' }); } }; return ( ); }; // ============================================ // Example 10: Clear Error // ============================================ export const ErrorHandlingExample = () => { const { error, clearError } = useAuthStore(); if (!error) return null; return (

{error}

); };