Connected login to backend

This commit is contained in:
Alex Holliday committed 2024-06-10 11:53:10 -07:00
1 parent 91891b1a90
commit 9beebd90a1
3 files changed
+70 -3

No files matched your search

+24 -1
View File
@@ -23,7 +23,12 @@ export const register = createAsyncThunk(
);
export const login = createAsyncThunk("auth/login", async (form, thunkApi) => {
console.log(form);
try {
const res = await axios.post(`${BASE_URL}/auth/login`, form);
return res.data;
} catch (error) {
return thunkApi.rejectWithValue(error.response.data);
}
});
const authSlice = createSlice({
@@ -32,6 +37,7 @@ const authSlice = createSlice({
reducers: {},
extraReducers: (builder) => {
builder
// Register thunk
.addCase(register.pending, (state, action) => {
state.isLoading = true;
})
@@ -47,6 +53,23 @@ const authSlice = createSlice({
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
// Login thunk
.addCase(login.pending, (state, action) => {
state.isLoading = true;
})
.addCase(login.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
const decodedToken = jwtDecode(action.payload.data);
const user = { ...decodedToken };
state.user = user;
})
.addCase(login.rejected, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
});
},
});