From 714b013d65da392e0dd73e2fc156ce62e9522965 Mon Sep 17 00:00:00 2001 From: serhiyserhiychuk <141840178+serhiyserhiychuk@users.noreply.github.com> Date: Thu, 13 Jun 2024 11:47:39 -0400 Subject: [PATCH] autologin logic --- src/components/RegistrationForm/RegistrationForm.jsx | 7 +++---- src/redux/auth/operations.js | 5 ++++- src/redux/auth/slice.js | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/RegistrationForm/RegistrationForm.jsx b/src/components/RegistrationForm/RegistrationForm.jsx index a27a491..220acc8 100644 --- a/src/components/RegistrationForm/RegistrationForm.jsx +++ b/src/components/RegistrationForm/RegistrationForm.jsx @@ -24,10 +24,9 @@ export const RegistrationForm = () => { const dispatch = useDispatch(); const navigate = useNavigate(); - const handleSubmit = (values, actions) => { - console.log(values); - dispatch(register(values)); - toast.success("Ти зареєструвався"); + const handleSubmit = async (values, actions) => { + await dispatch(register(values)).unwrap(); + toast.success("Registered successfully"); navigate("/home"); actions.resetForm(); }; diff --git a/src/redux/auth/operations.js b/src/redux/auth/operations.js index 5ac173c..3a2e9e6 100644 --- a/src/redux/auth/operations.js +++ b/src/redux/auth/operations.js @@ -17,7 +17,10 @@ export const register = createAsyncThunk( try { axios.defaults.headers.secretkey = "QWERTY"; await axios.post("/auth/register", userInfo); - const logResponse = await axios.post("/auth/login", userInfo); + const logResponse = await axios.post("/auth/login", { + email: userInfo.email, + password: userInfo.password, + }); setAuthHeader(logResponse.data.token); return logResponse.data; } catch (error) { diff --git a/src/redux/auth/slice.js b/src/redux/auth/slice.js index 9bf34ae..eb0d336 100644 --- a/src/redux/auth/slice.js +++ b/src/redux/auth/slice.js @@ -32,6 +32,7 @@ const authSlice = createSlice({ .addCase(register.fulfilled, (state, action) => { state.user = action.payload.user; state.token = action.payload.token; + state.isLoggedIn = true; }) .addCase(register.rejected, (state) => { state.loading = false;