Files
Checkmate/Client/src/Features/Auth/authSlice.js
T

257 lines
7.3 KiB
JavaScript
Raw Normal View History

import axiosInstance from "../../Utils/axiosConfig";
2024-06-07 16:17:10 -07:00
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { jwtDecode } from "jwt-decode";
const initialState = {
isLoading: false,
authToken: "",
2024-06-07 16:17:10 -07:00
user: "",
success: null,
msg: null,
};
export const register = createAsyncThunk(
"auth/register",
async (form, thunkApi) => {
try {
const res = await axiosInstance.post("/auth/register", form);
2024-06-07 16:17:10 -07:00
return res.data;
} catch (error) {
2024-06-25 11:14:16 -07:00
if (error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
2024-06-07 16:17:10 -07:00
}
}
);
2024-06-10 10:38:00 -07:00
export const login = createAsyncThunk("auth/login", async (form, thunkApi) => {
2024-06-10 11:53:10 -07:00
try {
const res = await axiosInstance.post(`/auth/login`, form);
2024-06-10 11:53:10 -07:00
return res.data;
} catch (error) {
2024-06-25 11:14:16 -07:00
if (error.response && error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
2024-06-10 11:53:10 -07:00
}
2024-06-10 10:38:00 -07:00
});
2024-06-24 21:32:30 -04:00
export const update = createAsyncThunk(
"auth/update",
async (data, thunkApi) => {
2024-07-04 20:47:23 -04:00
const { authToken: token, localData: form } = data;
const user = jwtDecode(token);
2024-06-24 21:32:30 -04:00
try {
//1.5s delay to show loading spinner
await new Promise((resolve) => setTimeout(resolve, 1500));
2024-07-05 19:52:29 -04:00
const fd = new FormData();
2024-07-22 12:40:44 -04:00
form.firstName && fd.append("firstName", form.firstName);
form.lastName && fd.append("lastName", form.lastName);
2024-07-10 18:26:05 -04:00
form.password && fd.append("password", form.password);
form.newPassword && fd.append("newPassword", form.newPassword);
if (form.file && form.file !== "") {
2024-07-09 17:01:07 -04:00
const imageResult = await axiosInstance.get(form.file, {
responseType: "blob",
2024-07-09 20:41:47 -04:00
baseURL: "",
});
fd.append("profileImage", imageResult.data);
}
form.deleteProfileImage &&
fd.append("deleteProfileImage", form.deleteProfileImage);
2024-07-05 19:52:29 -04:00
const res = await axiosInstance.post(`/auth/user/${user._id}`, fd, {
headers: {
Authorization: `Bearer ${token}`,
2024-07-03 17:49:40 -04:00
"Content-Type": "multipart/form-data",
},
});
2024-06-24 21:32:30 -04:00
return res.data;
} catch (error) {
2024-06-25 14:20:13 -04:00
if (error.response && error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
2024-06-24 21:32:30 -04:00
}
}
);
2024-07-09 19:11:16 -04:00
export const deleteUser = createAsyncThunk(
"auth/delete",
async (data, thunkApi) => {
const user = jwtDecode(data);
try {
const res = await axiosInstance.delete(`/auth/user/${user._id}`, {
2024-07-09 20:51:26 -04:00
headers: { Authorization: `Bearer ${data}` },
2024-07-09 19:11:16 -04:00
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
}
}
);
export const forgotPassword = createAsyncThunk(
"auth/forgotPassword",
async (form, thunkApi) => {
try {
const res = await axiosInstance.post("/auth/recovery/request", form);
return res.data;
} catch (error) {
if (error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
}
}
);
export const setNewPassword = createAsyncThunk(
"auth/setNewPassword",
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,
});
return res.data;
} catch (error) {
if (error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
}
}
);
2024-06-10 12:24:17 -07:00
const handleAuthFulfilled = (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.authToken = action.payload.data.token;
state.user = action.payload.data.user;
2024-06-10 12:24:17 -07:00
};
const handleAuthRejected = (state, action) => {
state.isLoading = false;
2024-06-25 11:14:16 -07:00
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to login or register";
2024-06-10 12:24:17 -07:00
};
const handleUpdateFulfilled = (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.user = action.payload.data;
2024-06-25 14:20:13 -04:00
};
const handleUpdateRejected = (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to update profile data.";
};
2024-07-09 19:11:16 -04:00
const handleDeleteFulfilled = (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
};
const handleDeleteRejected = (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload ? action.payload.msg : "Failed to delete account.";
};
const handleForgotFulfilled = (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
};
const handleForgotRejected = (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to send reset instructions.";
};
const handleNewPasswordRejected = (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
2024-07-16 19:19:24 -04:00
: "Failed to reset password.";
};
2024-06-10 12:24:17 -07:00
2024-06-07 16:17:10 -07:00
const authSlice = createSlice({
name: "auth",
initialState,
2024-06-24 11:21:30 -07:00
reducers: {
clearAuthState: (state) => {
state.authToken = "";
state.user = "";
state.isLoading = false;
state.success = true;
state.msg = "Logged out successfully";
},
},
2024-06-07 16:17:10 -07:00
extraReducers: (builder) => {
2024-07-09 19:11:16 -04:00
// Register thunk
2024-06-07 16:17:10 -07:00
builder
2024-06-24 11:21:30 -07:00
.addCase(register.pending, (state) => {
2024-06-07 16:17:10 -07:00
state.isLoading = true;
})
2024-06-10 12:24:17 -07:00
.addCase(register.fulfilled, handleAuthFulfilled)
2024-07-09 19:11:16 -04:00
.addCase(register.rejected, handleAuthRejected);
// Login thunk
builder
2024-06-24 11:21:30 -07:00
.addCase(login.pending, (state) => {
2024-06-10 11:53:10 -07:00
state.isLoading = true;
})
2024-06-10 12:24:17 -07:00
.addCase(login.fulfilled, handleAuthFulfilled)
2024-07-09 19:11:16 -04:00
.addCase(login.rejected, handleAuthRejected);
// Update thunk
builder
2024-06-24 21:32:30 -04:00
.addCase(update.pending, (state) => {
state.isLoading = true;
})
.addCase(update.fulfilled, handleUpdateFulfilled)
2024-06-25 14:20:13 -04:00
.addCase(update.rejected, handleUpdateRejected);
2024-07-09 19:11:16 -04:00
// Delete thunk
builder
.addCase(deleteUser.pending, (state) => {
state.isLoading = true;
})
.addCase(deleteUser.fulfilled, handleDeleteFulfilled)
.addCase(deleteUser.rejected, handleDeleteRejected);
// Forgot password thunk
builder
.addCase(forgotPassword.pending, (state) => {
state.isLoading = true;
})
.addCase(forgotPassword.fulfilled, handleForgotFulfilled)
.addCase(forgotPassword.rejected, handleForgotRejected);
// Set new password thunk
builder
.addCase(setNewPassword.pending, (state) => {
state.isLoading = true;
})
2024-07-16 17:24:48 -04:00
.addCase(setNewPassword.fulfilled, handleAuthFulfilled)
.addCase(setNewPassword.rejected, handleNewPasswordRejected);
2024-06-07 16:17:10 -07:00
},
});
export default authSlice.reducer;
2024-06-24 11:21:30 -07:00
export const { clearAuthState } = authSlice.actions;