Overview
TheuseAuth hook provides a complete authentication system including user registration, login, logout, and alert management. It integrates with React Router for navigation and manages user state through context and localStorage.
Hook Signature
Return Value
The hook returns an object containing the following properties:Functions
registerUser
data(Object): User registration datacorreo(string): User emailpassword(string): User passwordconfirmPassword(string): Password confirmation- Additional user fields (nombre, etc.)
resetFormCallback(Function, optional): Callback to reset the registration form
- Validates password confirmation
- Creates user account
- Automatically logs in the new user
- Saves user data to localStorage
- Redirects to
/chaton success - Displays alerts for errors or success
login
logindata(Object): Login credentialscorreo(string): User emailpassword(string): User password
resetFormCallback(Function, optional): Callback to reset the login form
- Authenticates user credentials
- Saves user data to context and localStorage
- Redirects to
/chaton success - Displays welcome message or error alert
logout
- Clears user from context
- Removes user data from localStorage
- Redirects to
/home
handleAlert
show(boolean): Whether to show the alertmessage(string): Alert message contentheader(string): Alert header/title
State Properties
showAlert
alertMessage
alertHeader
Usage Example
Registration Example
Dependencies
This hook requires:UserContextfor managing user stateuseLocalStoragehook for persisting user datareact-router-domfor navigationauthServicefor API calls (createUser,loginUser)
Notes
- Password validation is performed client-side before submission
- User data is persisted in localStorage for session management
- All authentication functions handle errors gracefully with alert messages
- Successful operations automatically redirect users to appropriate pages
