2024-03-27 01:37:35 +01:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import {
|
2024-03-31 23:45:25 +02:00
|
|
|
createUser as registerApi,
|
|
|
|
isLoggedIn,
|
|
|
|
login as loginApi,
|
|
|
|
logout as logoutApi,
|
2024-03-27 01:37:35 +01:00
|
|
|
} from "../api";
|
|
|
|
import { useQuery } from "../hooks";
|
|
|
|
|
|
|
|
import { useLocation, useNavigate } from "react-router-dom";
|
|
|
|
|
|
|
|
const initState = {
|
2024-03-31 23:45:25 +02:00
|
|
|
user: undefined,
|
2024-03-27 01:37:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export const AuthenticationContext = React.createContext({
|
2024-03-31 23:45:25 +02:00
|
|
|
...initState,
|
|
|
|
login: () => {},
|
|
|
|
register: () => {},
|
|
|
|
logout: () => {},
|
2024-03-27 01:37:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
export const AuthenticationProvider = ({ children }) => {
|
2024-03-31 23:45:25 +02:00
|
|
|
const navigate = useNavigate();
|
|
|
|
const location = useLocation();
|
|
|
|
const query = useQuery();
|
|
|
|
const [authState, setAuthState] = useState(initState);
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
const redirect = () => {
|
|
|
|
navigate(query.get("redirect_uri") || "/");
|
|
|
|
};
|
2024-03-27 01:37:35 +01:00
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
const login = async (email, password) => {
|
|
|
|
loginApi(email, password)
|
|
|
|
.then((user) => {
|
|
|
|
setAuthState({ user });
|
|
|
|
redirect();
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
console.error("ici ca catch fort");
|
|
|
|
setAuthState({ user: undefined });
|
|
|
|
});
|
|
|
|
};
|
2024-03-27 01:37:35 +01:00
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
const register = async (username, password, confirmation) => {
|
|
|
|
registerApi(username, password, confirmation).then((user) => {
|
|
|
|
setAuthState({ user });
|
|
|
|
redirect();
|
|
|
|
});
|
|
|
|
};
|
2024-03-27 01:37:35 +01:00
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
const logout = () => {
|
|
|
|
logoutApi().then(() => {
|
|
|
|
setAuthState(initState);
|
|
|
|
navigate(`/login`);
|
|
|
|
});
|
|
|
|
};
|
2024-03-27 01:37:35 +01:00
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
useEffect(() => {
|
|
|
|
isLoggedIn()
|
|
|
|
.then((user) => {
|
|
|
|
if (user === "Unauthorized") throw new Error("Unauthorized");
|
|
|
|
setAuthState({ user });
|
|
|
|
if (location.pathname === "/login") {
|
|
|
|
redirect();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setAuthState({ user: undefined });
|
2024-03-27 01:37:35 +01:00
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
if (!location.pathname.match(/^(\/|\/login|\/register)$/)) {
|
|
|
|
navigate(`/login?redirect_uri=${encodeURI(location.pathname)}`);
|
2024-03-27 01:37:35 +01:00
|
|
|
}
|
2024-03-31 23:45:25 +02:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setIsLoading(false);
|
|
|
|
});
|
|
|
|
}, []);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (location.pathname === "/logout") {
|
|
|
|
logout();
|
2024-03-27 01:37:35 +01:00
|
|
|
}
|
2024-03-31 23:45:25 +02:00
|
|
|
}, [location]);
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
return <p>Loading...</p>;
|
|
|
|
}
|
2024-03-27 01:37:35 +01:00
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
return (
|
|
|
|
<AuthenticationContext.Provider
|
|
|
|
value={{ ...authState, login, logout, register }}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</AuthenticationContext.Provider>
|
|
|
|
);
|
2024-03-27 01:37:35 +01:00
|
|
|
};
|