/**
* 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}
}
handleLogin(
'user@example.com',
'password123'
)}
disabled={isLoading}
>
{isLoading ? 'Đang xử lý...' : 'Đăng nhập'}
);
};
// ============================================
// 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 (
{isLoading ? 'Đang xử lý...' : 'Đăng ký'}
);
};
// ============================================
// 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 && (
)}
);
};
// ============================================
// Example 4: Logout Button
// ============================================
export const LogoutButtonExample = () => {
const { logout, isLoading } = useAuthStore();
const navigate = useNavigate();
const handleLogout = async () => {
await logout();
navigate('/login');
};
return (
{isLoading ? 'Đang xử lý...' : 'Đăng xuất'}
);
};
// ============================================
// 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 (
handleForgotPassword('user@example.com')
}
disabled={isLoading}
>
Gửi email đặt lại mật khẩu
);
};
// ============================================
// 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 (
handleResetPassword(
'reset-token-123',
'newpassword123'
)
}
disabled={isLoading}
>
Đặt lại mật khẩu
);
};
// ============================================
// Example 7: Conditional Rendering by Role
// ============================================
export const RoleBasedComponentExample = () => {
const { userInfo } = useAuthStore();
return (
{userInfo?.role === 'admin' && (
Admin Panel
)}
{userInfo?.role === 'staff' && (
Staff Tools
)}
{userInfo?.role === 'customer' && (
Customer Dashboard
)}
);
};
// ============================================
// 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 (
Cập nhật thông tin
);
};
// ============================================
// Example 10: Clear Error
// ============================================
export const ErrorHandlingExample = () => {
const { error, clearError } = useAuthStore();
if (!error) return null;
return (
);
};