From 518650ccd518c486d69e0a6bd94656fb6a640bb8 Mon Sep 17 00:00:00 2001 From: MehrdadAdabi <126083584+mehrdadAdabi@users.noreply.github.com> Date: Thu, 27 Nov 2025 16:25:27 +0330 Subject: [PATCH] feat(auth): Enhance login flow and user profile management Refactor user information handling and improve the post-login experience. * **Login Flow & User Profile:** * Introduced `UserInfoService` to centralize user data storage and retrieval from local storage, replacing direct `localStorage` access for user `person` data. * Modified the `LoginPage` to use `userInfoService.updateUserInfo()` after successful OTP verification. * Implemented immediate fetching of the full user profile (`fetchUserProfile`) after login to ensure up-to-date user details. * Adjusted post-login navigation logic: users with a complete profile (indicated by `userInfo?.username`) are now directed to the campaigns page, while those with incomplete profiles are guided to the profile page for completion. * **UI/UX Improvements:** * The `CustomRadio` component now displays its `value` as a fallback if no `label` is explicitly provided, improving usability for radio buttons. * Adjusted styling for form field labels in `DynamicForm` to include a bottom margin, enhancing visual separation and readability. --- src/core/components/base/radio.tsx | 2 +- src/core/service/user-info.service.ts | 2 + src/modules/auth/pages/login/index.tsx | 16 ++- src/modules/auth/service/auth.service.ts | 2 + .../pages/step-form/dynamic-form.tsx | 4 +- .../dashboard/pages/step-form/index.tsx | 105 ++++++++++++++++- src/modules/dashboard/pages/steps/index.tsx | 108 ++++++++++++------ .../dashboard/pages/steps/step.type.ts | 9 ++ .../dashboard/service/campaigns.service.ts | 10 +- src/modules/dashboard/service/user.service.ts | 2 +- 10 files changed, 206 insertions(+), 54 deletions(-) diff --git a/src/core/components/base/radio.tsx b/src/core/components/base/radio.tsx index 1239938..b4ca4a4 100644 --- a/src/core/components/base/radio.tsx +++ b/src/core/components/base/radio.tsx @@ -77,7 +77,7 @@ const CustomRadio = React.forwardRef( : "text-foreground hover:text-foreground/80" )} > - {label} + {label ?? value} {error && ( diff --git a/src/core/service/user-info.service.ts b/src/core/service/user-info.service.ts index 568420b..7a5a548 100644 --- a/src/core/service/user-info.service.ts +++ b/src/core/service/user-info.service.ts @@ -22,6 +22,8 @@ class UserInfoService { const tokenObj = JSON.parse(tokenStr); return tokenObj || null; } + + } export const userInfoService = new UserInfoService(); diff --git a/src/modules/auth/pages/login/index.tsx b/src/modules/auth/pages/login/index.tsx index 21da5de..37e4563 100644 --- a/src/modules/auth/pages/login/index.tsx +++ b/src/modules/auth/pages/login/index.tsx @@ -17,11 +17,13 @@ import { CustomInput } from "@/core/components/base/input"; import { OTPDialog } from "@modules/auth/components/otp/opt-dialog"; import { toast } from "react-toastify"; +import { userInfoService } from "@/core/service/user-info.service"; import { DASHBOARD_ROUTE } from "@/modules/dashboard/routes/route.constant"; +import { fetchUserProfile } from "@/modules/dashboard/service/user.service"; +import { sendOtpService, verifyOtpService } from "@modules/auth/service/auth.service"; import { useMutation } from "@tanstack/react-query"; import { useState } from "react"; import { useNavigate } from "react-router-dom"; -import { sendOtpService, verifyOtpService } from "../../service/auth.service"; export function LoginPage() { const navigate = useNavigate(); @@ -71,7 +73,7 @@ export function LoginPage() { const verifyOtpMutation = useMutation({ mutationFn: verifyOtpService, - onSuccess: (data) => { + onSuccess: async(data) => { setSubmitLoading(false); if (data.resultType !== 0) { toast.error(data.message); @@ -81,14 +83,16 @@ export function LoginPage() { const person = JSON.parse(data.data).Person; const token = JSON.parse(data.data).Token; localStorage.setItem("token", JSON.stringify(token)); - localStorage.setItem("person", JSON.stringify(person)); + userInfoService.updateUserInfo(person) + await fetchUserProfile() + const userInfo = userInfoService.getUserInfo() setOtpDialog(false); - if (person.NationalCode === "") { - navigate(`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.profile}`, { + if (userInfo?.username) { + navigate(`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.campaigns}`, { replace: true, }); } else { - navigate(`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.campaigns}`, { + navigate(`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.profile}`, { replace: true, }); } diff --git a/src/modules/auth/service/auth.service.ts b/src/modules/auth/service/auth.service.ts index b3aa466..2bc1610 100644 --- a/src/modules/auth/service/auth.service.ts +++ b/src/modules/auth/service/auth.service.ts @@ -16,3 +16,5 @@ export const verifyOtpService = async ({ const res = await api.post(API_ADDRESS.auth.verifyOtp, { mobile, code }); return res.data; }; + + diff --git a/src/modules/dashboard/pages/step-form/dynamic-form.tsx b/src/modules/dashboard/pages/step-form/dynamic-form.tsx index fd4de8a..2ca0fa8 100644 --- a/src/modules/dashboard/pages/step-form/dynamic-form.tsx +++ b/src/modules/dashboard/pages/step-form/dynamic-form.tsx @@ -242,8 +242,8 @@ const DynamicForm: FC = ({ fields, processId, stepId }) => { key={field.ID} className={`col-span-1 md:col-span-1 ${colSpanClass}`} > -