Skip to main content

Overview

The AuthContext manages user authentication, login state, and role-based access control. It handles login/logout operations and persists authentication state to localStorage. Source: ~/workspace/source/src/context/AuthContext.jsx

Setup

Wrap your application with the AuthProvider (must be inside Router):

Usage

Use the useAuth custom hook to access authentication context:

State Properties

isAuthenticated

boolean
Whether a user is currently logged in. Synced with localStorage on mount.

role

string
Current user’s role. Either 'admin' or 'cliente'. Empty string when not authenticated.

email

string
Email input value for the login form.

setEmail

function
Update the email input value.

password

string
Password input value for the login form.

setPassword

function
Update the password input value.

errors

object
Validation and authentication error messages. Keys: email, password.

setErrors

function
Update error messages. Useful for clearing errors.

setIsAuth

function
Manually set authentication state. Used for logout.

Methods

handleSubmit

Processes login form submission, validates credentials, and redirects based on user role.
Event
required
Form submit event. Will be prevented from default behavior.
Behavior:
  1. Validates email and password are not empty
  2. Fetches user data from data/users.json
  3. Matches credentials against user database
  4. Sets authentication state and role
  5. Saves to localStorage: isAuth and role
  6. Redirects:
    • Admin users → /admin
    • Regular users → /
Validation Errors:
  • Empty email: "Email es requerido"
  • Empty password: "Password es requerido"
  • Invalid credentials: "credenciales invalidas"
  • Server error: "Algo salió mal. Por favor, inténtalo de nuevo más tarde."
Example:
src/layout/Login.jsx

Authentication Flow

Login Process

  1. User enters email and password
  2. Form submission calls handleSubmit(e)
  3. Client-side validation checks for empty fields
  4. Fetches user database from data/users.json
  5. Searches for matching email and password
  6. On success:
    • Sets isAuthenticated = true
    • Sets role based on user data
    • Saves to localStorage
    • Navigates to appropriate route
  7. On failure:
    • Sets error message
    • User remains on login page

Auto-Login on Mount

The context checks localStorage on mount:
This enables persistent sessions across page refreshes.

Logout Implementation

To implement logout, clear localStorage and reset auth state:

Protected Routes

Use authentication state to protect routes:
src/App.jsx

User Data Structure

The authentication system expects a data/users.json file with this structure:

Security Notes

This implementation stores passwords in plain text and uses client-side validation only. This is suitable for demonstration purposes but NOT for production use.For production:
  • Implement server-side authentication
  • Hash passwords with bcrypt or similar
  • Use secure tokens (JWT)
  • Implement proper session management
  • Add HTTPS/TLS encryption

Complete Implementation Example

src/App.jsx