From 95842a7b43d40183f2a5507e03778051a0ef9661 Mon Sep 17 00:00:00 2001 From: Mohammed Al-yaseen Date: Fri, 31 Oct 2025 14:14:43 +0300 Subject: [PATCH] otp: register the token after success login --- src/features/auth/authSlice.ts | 24 ++++++++---------------- src/pages/otp/page.tsx | 10 ++++------ src/utils/types/appTypes.ts | 8 +------- 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/features/auth/authSlice.ts b/src/features/auth/authSlice.ts index fbdd31d..0eb55b3 100644 --- a/src/features/auth/authSlice.ts +++ b/src/features/auth/authSlice.ts @@ -1,11 +1,11 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +import { ConfirmOTPResponse } from "pages/otp/types"; import { baseApi } from "redux/api/apiSlice"; import { AppThunk } from "redux/store"; -import { ACCESS_TOKEN, USER_NAME } from "utils/constants"; -import { LoginResponseType, UserType } from "utils/types/appTypes"; +import { ACCESS_TOKEN } from "utils/constants"; export type AuthState = { - user: UserType | undefined; + user: any; isActivated: boolean; loading: boolean; loginFailed: boolean; @@ -43,19 +43,11 @@ export const authSlice = createSlice({ state.loginFailed = false; state.loaded = false; }, - loginSuccess(state, action: PayloadAction) { - if (action.payload.token) { - localStorage.setItem(ACCESS_TOKEN, action.payload.token); - state.token = action.payload.token; - if (state.user?.username) - localStorage.setItem(USER_NAME, state.user.username); - - state.isActivated = true; - state.loading = false; - state.loginFailed = false; - state.expiredToken = false; - state.error = ""; - state.loaded = true; + loginSuccess(state, action: PayloadAction) { + if (action.payload.result.access_token) { + localStorage.setItem(ACCESS_TOKEN, action.payload.result.access_token); + state.token = action.payload.result.access_token; + localStorage.setItem("customer", JSON.stringify(action.payload.result.customer)); } }, logout: (state) => { diff --git a/src/pages/otp/page.tsx b/src/pages/otp/page.tsx index fe6b119..57998ba 100644 --- a/src/pages/otp/page.tsx +++ b/src/pages/otp/page.tsx @@ -3,15 +3,17 @@ import OtpIcon from "components/Icons/otpIcon.tsx"; import OtpInput from "components/OtpInput/OtpInput"; import ProText from "components/ProText"; import ProTitle from "components/ProTitle"; +import { loginSuccess } from "features/auth/authSlice"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { useNavigate, useParams } from "react-router-dom"; import { useConfirmOtpMutation, useSendOtpMutation } from "redux/api/auth"; +import { useAppDispatch } from "redux/hooks"; import { ProGray1 } from "ThemeConstants"; -import { ACCESS_TOKEN } from "utils/constants"; export default function OtpPage() { const { subdomain } = useParams(); + const dispatch = useAppDispatch(); const { t } = useTranslation(); const [sendOtp, { isLoading }] = useSendOtpMutation(); const [confirmOtp, { isLoading: isConfirmLoading }] = useConfirmOtpMutation(); @@ -81,11 +83,7 @@ export default function OtpPage() { confirmOtp({ otp: otp, phone: localStorage.getItem("userPhone") }) .unwrap() .then((response) => { - localStorage.setItem( - "customer", - JSON.stringify(response.result.customer), - ); - localStorage.setItem(ACCESS_TOKEN, response.result.access_token); + dispatch(loginSuccess(response)); message.info(t("otp.confirmOTPSuccess")); navigate(`/${subdomain}/menu`); }); diff --git a/src/utils/types/appTypes.ts b/src/utils/types/appTypes.ts index 7fac6e8..adb8ec4 100644 --- a/src/utils/types/appTypes.ts +++ b/src/utils/types/appTypes.ts @@ -132,13 +132,7 @@ export type LinkParameterType = { sign: "=" | ">" | ">=" | "<" | "<="; value: string; }; - -export type LoginResponseType = { - status: string; - token?: string; - error?: string; -}; - + export type LoginUserType = { username?: string; password?: string;