import React, { createContext, useState, useContext } from 'react';
import PropTypes from 'prop-types';
/**
* Context for managing authentication state and actions.
* @constant {Object}
*/
const AuthContext = createContext();
/**
* Provider component for managing authentication state and actions.
* @param {Object} props - The properties passed to the component.
* @param {ReactNode} props.children - The child components wrapped by the AuthProvider.
* @returns {JSX.Element} The JSX element representing the AuthProvider component.
*/
export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);
/**
* Sets the user data upon successful login.
* @param {Object} userData - The user data obtained after login.
* @returns {void}
*/
const login = (userData) => {
setUser(userData);
};
/**
* Clears the user data upon logout.
* @returns {void}
*/
const logout = () => {
setUser(null);
};
return (
<AuthContext.Provider value={{ user, login, logout }}>
{children}
</AuthContext.Provider>
);
};
AuthProvider.propTypes = {
children: PropTypes.node.isRequired,
};
/**
* Custom hook for accessing authentication context.
* @returns {Object} An object containing user data and authentication methods.
*/
export const useAuth = () => useContext(AuthContext);