diff --git a/src/components/contexts/UserContext.jsx b/src/components/contexts/UserContext.jsx
index f9b2292..8a4a4c4 100644
--- a/src/components/contexts/UserContext.jsx
+++ b/src/components/contexts/UserContext.jsx
@@ -4,97 +4,112 @@ const AUTH_URL = 'https://dev-auth.fiskerdps.com';
const UserContext = React.createContext();
export const UserProvider = ({ children }) => {
- const [fetching, setFetching] = useState(false);
- const [signedIn, setSignedIn] = useState(false);
- const [user, setUser] = useState(null);
- const [token, setToken] = useState(null);
- const [error, setError] = useState(null);
+ const [fetching, setFetching] = useState(false);
+ const [token, setToken] = useState(null);
+ const [error, setError] = useState(null);
- const signIn = (username, password) => {
- if (!username) throw new Error('Email is required');
- if (!password) throw new Error('Password is required');
- setFetching(true);
- setError(null);
- return fetch(`${AUTH_URL}/auth/login`, {
- method: "POST",
- mode: "no-cors",
- headers: {
- "Content-Type":"text/plain"
- },
- body: JSON.stringify({
- username,
- password,
- })
- })
- .then((response) => {
- debugger;
- return response.json();
- })
- .then((result) => {
- debugger;
- setFetching(false);
- if (result.error) setError(result.error);
- return result;
- })
- .catch((error) => {
- setError(error.message);
- setFetching(false);
- return null;
- });
- };
+ useEffect(async () => {
+ if (!sessionStorage) return;
+ const token = JSON.parse(sessionStorage.getItem("token"));
+ if (token === null) return;
+ const { accessToken: { jwtToken }} = token;
+ const resp = await fetch(`${AUTH_URL}/auth/verify`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify({ token: jwtToken })
+ });
+ const result = await resp.json();
+ if (!result.authenticated) return;
+ setToken(token);
+ }, []);
- const signUp = (username, password, confirmPassword) => {
- if (!username) throw new Error('Email is required');
- if (!password) throw new Error('Password is required');
- if (password !== confirmPassword) throw new Error('Passwords do not match');
- setFetching(true);
- setError(null);
- return fetch(`${AUTH_URL}/auth/register`, {
- method: "POST",
- mode: "cors",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify({
- username,
- password,
- })
- })
- .then((response) => {
- debugger;
- return response.json();
- })
- .then((result) => {
- if (result.error) setError(result.error);
- setFetching(false);
- return result;
- })
- .catch((error) => {
- setError(error.message);
- setFetching(false);
- return null;
- });
- };
+ const requestSignIn = (username, password) => fetch(`${AUTH_URL}/auth/login`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify({
+ username,
+ password,
+ })
+ })
+ .then((response) => response.json());
+
+ const requestSignUp = (username, password) => fetch(`${AUTH_URL}/auth/register`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify({
+ username,
+ password,
+ })
+ })
+ .then((response) => response.json());
+
+ const signIn = (username, password) => {
+ if (!username) throw new Error('Email is required');
+ if (!password) throw new Error('Password is required');
+ setFetching(true);
+ setError(null);
+ return requestSignIn(username, password)
+ .then((result) => {
+ setFetching(false);
+ if (result.message) throw new Error(result.message);
+ signedIn(result);
+ return result;
+ })
+ .catch((error) => {
+ setError(error.message);
+ setFetching(false);
+ return null;
+ });
+ };
- const signOut = async () => {
- setUser(null);
- };
+ const signUp = (username, password, confirmPassword) => {
+ if (!username) throw new Error('Email is required');
+ if (!password) throw new Error('Password is required');
+ if (password !== confirmPassword) throw new Error('Passwords do not match');
+ setFetching(true);
+ setError(null);
+ return requestSignUp(username, password)
+ .then((result) => {
+ if (result.message) throw new Error(result.message);
+ })
+ .catch((error) => {
+ setError(error.message);
+ setFetching(false);
+ return null;
+ });
+ };
- return (
-
- {children}
-
- );
+ const signOut = async () => {
+ setToken(null);
+ if (!sessionStorage) return;
+ sessionStorage.removeItem("token");
+ };
+
+ const signedIn = (token) => {
+ setToken(token);
+ if (!sessionStorage) return;
+ sessionStorage.setItem("token", JSON.stringify(token));
+ }
+
+ return (
+
+ {children}
+
+ );
};
export const useUserContext = () => useContext(UserContext);