This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/website/contexts/UserContext.js

56 lines
1.7 KiB
JavaScript

import { createContext, useState, useEffect } from 'react';
import Cookies from "universal-cookie";
import api from '../utils/api';
const cookies = new Cookies();
export const UserContext = createContext();
function UserContextProvider(props) {
const [user, setUser] = useState(null);
const [isAuth, setIsAuth] = useState(false);
const [loginLoading, setLoginLoading] = useState(false);
const [messageLogin, setMessageLogin] = useState("");
useEffect(() => {
const user = cookies.get('user');
setUser(user);
if (user != undefined) {
setIsAuth(true);
}
}, []);
const loginUser = ({ email, password }) => {
setLoginLoading(true);
api.post('/users/login', { email, password })
.then((response) => {
const user = response.data;
cookies.set('user', user);
setUser(user);
setIsAuth(true);
setMessageLogin('<p class="form-success"><b>Succès:</b> Connexion réussi!</p>');
setLoginLoading(false);
})
.catch((error) => {
setMessageLogin(`<p class="form-error"><b>Erreur:</b> ${error.response.data.message}</p>`);
setLoginLoading(false);
setIsAuth(false);
setUser(null);
});
}
const logoutUser = () => {
cookies.remove('user');
setUser(null);
setIsAuth(false);
}
return (
<UserContext.Provider value={{ user, loginUser, logoutUser, loginLoading, messageLogin, isAuth }}>
{props.children}
</UserContext.Provider>
);
}
export default UserContextProvider;