/**
* Example: How to use useAuthStore in components
*
* This file is for reference only, should not be used
* in 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 has been handled in 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 Please login
;
}
return (
User Information
Name: {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 (
);
};
// ============================================
// Example 5: Forgot Password
// ============================================
export const ForgotPasswordExample = () => {
const { forgotPassword, isLoading } = useAuthStore();
const handleForgotPassword = async (
email: string
) => {
try {
await forgotPassword({ email });
// Toast will display success message
} 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 Loading...
;
}
if (!isAuthenticated || !token) {
return You are not logged in
;
}
return You are logged in
;
};
// ============================================
// 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 (
);
};