Wire up auth api calls
This commit is contained in:
@@ -4,97 +4,112 @@ const AUTH_URL = 'https://dev-auth.fiskerdps.com';
|
|||||||
const UserContext = React.createContext();
|
const UserContext = React.createContext();
|
||||||
|
|
||||||
export const UserProvider = ({ children }) => {
|
export const UserProvider = ({ children }) => {
|
||||||
const [fetching, setFetching] = useState(false);
|
const [fetching, setFetching] = useState(false);
|
||||||
const [signedIn, setSignedIn] = useState(false);
|
const [token, setToken] = useState(null);
|
||||||
const [user, setUser] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
const [token, setToken] = useState(null);
|
|
||||||
const [error, setError] = useState(null);
|
|
||||||
|
|
||||||
const signIn = (username, password) => {
|
useEffect(async () => {
|
||||||
if (!username) throw new Error('Email is required');
|
if (!sessionStorage) return;
|
||||||
if (!password) throw new Error('Password is required');
|
const token = JSON.parse(sessionStorage.getItem("token"));
|
||||||
setFetching(true);
|
if (token === null) return;
|
||||||
setError(null);
|
const { accessToken: { jwtToken }} = token;
|
||||||
return fetch(`${AUTH_URL}/auth/login`, {
|
const resp = await fetch(`${AUTH_URL}/auth/verify`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
mode: "no-cors",
|
headers: {
|
||||||
headers: {
|
"Content-Type": "application/json"
|
||||||
"Content-Type":"text/plain"
|
},
|
||||||
},
|
body: JSON.stringify({ token: jwtToken })
|
||||||
body: JSON.stringify({
|
});
|
||||||
username,
|
const result = await resp.json();
|
||||||
password,
|
if (!result.authenticated) return;
|
||||||
})
|
setToken(token);
|
||||||
})
|
}, []);
|
||||||
.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;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const signUp = (username, password, confirmPassword) => {
|
const requestSignIn = (username, password) => fetch(`${AUTH_URL}/auth/login`, {
|
||||||
if (!username) throw new Error('Email is required');
|
method: "POST",
|
||||||
if (!password) throw new Error('Password is required');
|
headers: {
|
||||||
if (password !== confirmPassword) throw new Error('Passwords do not match');
|
"Content-Type": "application/json"
|
||||||
setFetching(true);
|
},
|
||||||
setError(null);
|
body: JSON.stringify({
|
||||||
return fetch(`${AUTH_URL}/auth/register`, {
|
username,
|
||||||
method: "POST",
|
password,
|
||||||
mode: "cors",
|
})
|
||||||
headers: {
|
})
|
||||||
"Content-Type": "application/json"
|
.then((response) => response.json());
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
const requestSignUp = (username, password) => fetch(`${AUTH_URL}/auth/register`, {
|
||||||
username,
|
method: "POST",
|
||||||
password,
|
headers: {
|
||||||
})
|
"Content-Type": "application/json"
|
||||||
})
|
},
|
||||||
.then((response) => {
|
body: JSON.stringify({
|
||||||
debugger;
|
username,
|
||||||
return response.json();
|
password,
|
||||||
})
|
})
|
||||||
.then((result) => {
|
})
|
||||||
if (result.error) setError(result.error);
|
.then((response) => response.json());
|
||||||
setFetching(false);
|
|
||||||
return result;
|
const signIn = (username, password) => {
|
||||||
})
|
if (!username) throw new Error('Email is required');
|
||||||
.catch((error) => {
|
if (!password) throw new Error('Password is required');
|
||||||
setError(error.message);
|
setFetching(true);
|
||||||
setFetching(false);
|
setError(null);
|
||||||
return 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 () => {
|
const signUp = (username, password, confirmPassword) => {
|
||||||
setUser(null);
|
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 (
|
const signOut = async () => {
|
||||||
<UserContext.Provider value={{
|
setToken(null);
|
||||||
fetching,
|
if (!sessionStorage) return;
|
||||||
signedIn,
|
sessionStorage.removeItem("token");
|
||||||
user,
|
};
|
||||||
token,
|
|
||||||
error,
|
const signedIn = (token) => {
|
||||||
setError,
|
setToken(token);
|
||||||
signIn,
|
if (!sessionStorage) return;
|
||||||
signUp,
|
sessionStorage.setItem("token", JSON.stringify(token));
|
||||||
signOut,
|
}
|
||||||
}}>
|
|
||||||
{children}
|
return (
|
||||||
</UserContext.Provider>
|
<UserContext.Provider value={{
|
||||||
);
|
fetching,
|
||||||
|
token,
|
||||||
|
error,
|
||||||
|
setError,
|
||||||
|
signIn,
|
||||||
|
signUp,
|
||||||
|
signOut,
|
||||||
|
}}>
|
||||||
|
{children}
|
||||||
|
</UserContext.Provider>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useUserContext = () => useContext(UserContext);
|
export const useUserContext = () => useContext(UserContext);
|
||||||
|
|||||||
Reference in New Issue
Block a user