Finish refactoring auth slice, fixed several bugs

This commit is contained in:
Alex Holliday
2024-08-17 20:23:34 -07:00
parent 46bd317760
commit 6d99002fd1
6 changed files with 59 additions and 22 deletions
+6 -20
View File
@@ -51,9 +51,6 @@ export const update = createAsyncThunk(
const { authToken: token, localData: form } = data;
const user = jwtDecode(token);
try {
//1.5s delay to show loading spinner
await new Promise((resolve) => setTimeout(resolve, 1500));
const fd = new FormData();
form.firstName && fd.append("firstName", form.firstName);
form.lastName && fd.append("lastName", form.lastName);
@@ -69,12 +66,8 @@ export const update = createAsyncThunk(
form.deleteProfileImage &&
fd.append("deleteProfileImage", form.deleteProfileImage);
const res = await axiosInstance.put(`/auth/user/${user._id}`, fd, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "multipart/form-data",
},
});
const res = await axiosInstance.updateUser(token, user._id, fd);
return res.data;
} catch (error) {
if (error.response && error.response.data) {
@@ -95,9 +88,7 @@ export const deleteUser = createAsyncThunk(
const user = jwtDecode(data);
try {
const res = await axiosInstance.delete(`/auth/user/${user._id}`, {
headers: { Authorization: `Bearer ${data}` },
});
const res = await axiosInstance.deleteUser(data, user._id);
return res.data;
} catch (error) {
if (error.response && error.response.data) {
@@ -116,7 +107,7 @@ export const forgotPassword = createAsyncThunk(
"auth/forgotPassword",
async (form, thunkApi) => {
try {
const res = await axiosInstance.post("/auth/recovery/request", form);
const res = await axiosInstance.forgotPassword(form);
return res.data;
} catch (error) {
if (error.response.data) {
@@ -136,13 +127,8 @@ export const setNewPassword = createAsyncThunk(
async (data, thunkApi) => {
const { token, form } = data;
try {
await axiosInstance.post("/auth/recovery/validate", {
recoveryToken: token,
});
const res = await axiosInstance.post("/auth/recovery/reset", {
...form,
recoveryToken: token,
});
await axiosInstance.validateRecoveryToken(token);
const res = await axiosInstance.setNewPassword(token, form);
return res.data;
} catch (error) {
if (error.response.data) {
+47
View File
@@ -104,4 +104,51 @@ axiosInstance.loginUser = async (form) => {
return axiosInstance.post(`/auth/login`, form);
};
// **********************************
// Update in an exisiting user
// **********************************
axiosInstance.updateUser = async (authToken, userId, form) => {
return axiosInstance.put(`/auth/user/${userId}`, form, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Delete an exisiting user
// **********************************
axiosInstance.deleteUser = async (authToken, userId) => {
return axiosInstance.delete(`/auth/user/${userId}`, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Forgot password request
// **********************************
axiosInstance.forgotPassword = async (form) => {
return axiosInstance.post(`/auth/recovery/request`, form);
};
axiosInstance.validateRecoveryToken = async (recoveryToken) => {
return axiosInstance.post("/auth/recovery/validate", {
recoveryToken,
});
};
// **********************************
// Set new password request
// **********************************
axiosInstance.setNewPassword = async (recoveryToken, form) => {
return axiosInstance.post("/auth/recovery/reset", {
...form,
recoveryToken,
});
};
export default axiosInstance;