Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions src/features/auth/authSlice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSlice } from '@reduxjs/toolkit';
import { registerUser, loginUser, logoutUser, verifyEmail, resendEmailVerification, forgotPassword } from './authThunks';

const initialState = {
user: null,
Expand Down Expand Up @@ -34,6 +35,84 @@ const authSlice = createSlice({
state.isLoading = false;
},
},
extraReducers: (builder) => {
builder
.addCase(registerUser.pending, (state) => {
state.isLoading = true;
state.error = null;
})
.addCase(registerUser.fulfilled, (state) => {
state.isLoading = false;
})
.addCase(registerUser.rejected, (state, action) => {
state.isLoading = false;
state.error = action.payload;
})
.addCase(loginUser.pending, (state) => {
state.isLoading = true;
state.error = null;
})
.addCase(loginUser.fulfilled, (state, action) => {
state.isLoading = false;
state.user = action.payload.user;
state.token = action.payload.token;
state.isAuthenticated = true;
state.error = null;
})
.addCase(loginUser.rejected, (state, action) => {
state.isLoading = false;
state.error = action.payload;
})
.addCase(logoutUser.pending, (state) => {
state.isLoading = true;
})
.addCase(logoutUser.fulfilled, (state) => {
state.user = null;
state.token = null;
state.isAuthenticated = false;
state.isLoading = false;
state.error = null;
})
.addCase(logoutUser.rejected, (state, action) => {
state.isLoading = false;
state.error = action.payload;
})
.addCase(verifyEmail.pending, (state) => {
state.isLoading = true;
state.error = null;
})
.addCase(verifyEmail.fulfilled, (state) => {
state.isLoading = false;
state.error = null;
})
.addCase(verifyEmail.rejected, (state, action) => {
state.isLoading = false;
state.error = action.payload;
})
.addCase(resendEmailVerification.pending, (state) => {
state.isLoading = true;
state.error = null;
})
.addCase(resendEmailVerification.fulfilled, (state) => {
state.isLoading = false;
state.error = null;
})
.addCase(resendEmailVerification.rejected, (state, action) => {
state.isLoading = false;
state.error = action.payload;
})
.addCase(forgotPassword.pending, (state) => {
state.isLoading = true;
state.error = null;
})
.addCase(forgotPassword.fulfilled, (state) => {
state.isLoading = false;
})
.addCase(forgotPassword.rejected, (state, action) => {
state.isLoading = false;
state.error = action.payload;
});
},
});

export const { setCredentials, clearCredentials, setAuthLoading, setAuthError } = authSlice.actions;
Expand Down
14 changes: 14 additions & 0 deletions src/features/auth/authThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ export const verifyEmail = createAsyncThunk<any, string, { rejectValue: RejectVa
}
);

export const resendEmailVerification = createAsyncThunk<any, string, { rejectValue: RejectValue }>(
'auth/resendEmailVerification',
async (email, { rejectWithValue }) => {
try {
const response = await api.post('/auth/resend-verification', { email });
toastSuccess('Verification email sent');
return response.data;
} catch (err: any) {
toastError(err);
return rejectWithValue(err.response?.data || err.message);
}
}
);

export const forgotPassword = createAsyncThunk<any, string, { rejectValue: RejectValue }>(
'auth/forgotPassword',
async (email) => {
Expand Down
Loading