Source

hooks/useAuth.js

import { useContext } from 'react';
import { AuthContext } from '../contexts/AuthContext';

/**
 * Custom hook to access the authentication context.
 * It provides access to the authentication state and functions provided by the AuthProvider.
 *
 * @throws {Error} If used outside of an AuthProvider.
 * @returns {object} The authentication context object.
 */
export const useAuth = () => {
  const context = useContext(AuthContext);
  if (!context) {
    throw new Error('useAuth must be used within an AuthProvider');
  }
  return context;
};