From 408fb1d350611d11adbdbbe5e159d6e04cb94a1c Mon Sep 17 00:00:00 2001 From: Rohit Date: Fri, 14 Jun 2024 14:20:45 +0530 Subject: [PATCH] feature/google-login --- app/(root)/layout.tsx | 1 - .../custom/forms/ProfileInitialForm.tsx | 4 ++-- .../functional/RedirectProfileNotAdded.tsx | 2 +- constants/ServiceConstants.tsx | 1 + providers/RefreshTokenProvider.tsx | 8 +++++-- service/UserProviderApplication.tsx | 22 +++++++++++++------ 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/(root)/layout.tsx b/app/(root)/layout.tsx index fc69cf5..686d2d4 100644 --- a/app/(root)/layout.tsx +++ b/app/(root)/layout.tsx @@ -41,7 +41,6 @@ export default function AuthLayout({ - {children} diff --git a/components/custom/forms/ProfileInitialForm.tsx b/components/custom/forms/ProfileInitialForm.tsx index bfc6428..8d87a41 100644 --- a/components/custom/forms/ProfileInitialForm.tsx +++ b/components/custom/forms/ProfileInitialForm.tsx @@ -12,6 +12,7 @@ import {dispatchError} from "@/utils/DispatchFunctions"; import {useMutation} from "@tanstack/react-query"; import {redirect, useRouter} from "next/navigation"; import AuthenticatedAxiosInterceptor from "@/utils/AuthenticatedAxiosInterceptor"; +import {BLOG_PAGE} from "@/constants/UiPathConstants"; export default function ProfileInitialForm() { @@ -32,7 +33,6 @@ export default function ProfileInitialForm() { const {isAuthenticated, authorities} = useSelector((state: RootState) => state.auth); const [options, setOptions] = useState([]); const dispatch = useDispatch(); - const router = useRouter(); const authAxios = AuthenticatedAxiosInterceptor(); const addProfileMutation = useMutation({ @@ -47,7 +47,7 @@ export default function ProfileInitialForm() { useLayoutEffect(()=> { if (isAuthenticated && !authorities?.includes('ROLE_DUMMY')) { - redirect("/dashboard"); + redirect("/"); } else if (!isAuthenticated) { redirect("/login"); } diff --git a/components/functional/RedirectProfileNotAdded.tsx b/components/functional/RedirectProfileNotAdded.tsx index 5a81f4d..472928c 100644 --- a/components/functional/RedirectProfileNotAdded.tsx +++ b/components/functional/RedirectProfileNotAdded.tsx @@ -9,7 +9,7 @@ const RedirectProfileNotAdded: React.FC = () => { const {isAuthenticated, authorities} = useSelector((state: RootState) => state.auth); - useEffect(()=> { + useLayoutEffect(()=> { if (isAuthenticated && authorities?.includes('ROLE_DUMMY')) { redirect("/profile-addition-initial"); } diff --git a/constants/ServiceConstants.tsx b/constants/ServiceConstants.tsx index ec41568..2a9906f 100644 --- a/constants/ServiceConstants.tsx +++ b/constants/ServiceConstants.tsx @@ -3,3 +3,4 @@ export const EMAIL_REGEX: RegExp = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{ export const UUID_REGEX: RegExp = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/; export const ACCOUNT_INACTIVE_ERROR_CODE: string = "DE__AUTH-2004"; export const chipColors = ['bg-purple-700', 'bg-red-700', 'bg-amber-700', 'bg-blue-700']; +export const PROFILE_NOT_FOUND_CODE = "DE__USER-2016"; diff --git a/providers/RefreshTokenProvider.tsx b/providers/RefreshTokenProvider.tsx index e538fb1..6e1b731 100644 --- a/providers/RefreshTokenProvider.tsx +++ b/providers/RefreshTokenProvider.tsx @@ -4,16 +4,20 @@ import * as React from "react"; import {useLayoutEffect, useState} from "react"; import LoaderPage from "@/components/custom/loaders/LoaderPage"; import {refreshToken} from "@/rest/AuthProviderApplication"; -import {useDispatch} from "react-redux"; +import {useDispatch, useSelector} from "react-redux"; import {clearCredentials, setCredentials} from "@/state/authSlice"; // @ts-ignore import Cookies from 'js-cookie'; import {addUserProfile} from "@/service/UserProviderApplication"; +import {RootState} from "@/state/store"; +import {redirect, useRouter} from "next/navigation"; export function RefreshTokenProvider({children}: Readonly<{ children: React.ReactNode }>) { const [isChecking, setIsChecking] = useState(true); + const {authorities} = useSelector((state: RootState) => state.auth); const dispatch = useDispatch(); + const router = useRouter(); useLayoutEffect(() => { let isMounted = true; @@ -32,7 +36,7 @@ export function RefreshTokenProvider({children}: Readonly<{ children: React.Reac Cookies.set(process.env.NEXT_PUBLIC_COOKIE_TOKEN_NAME, response.headers[`${process.env.NEXT_PUBLIC_COOKIE_HEADER_NAME}`], { expires: 1, }); - addUserProfile(dispatch); + addUserProfile(dispatch, router, response.data.authorities); setIsChecking(false); } }) diff --git a/service/UserProviderApplication.tsx b/service/UserProviderApplication.tsx index bdfa57a..e690062 100644 --- a/service/UserProviderApplication.tsx +++ b/service/UserProviderApplication.tsx @@ -2,12 +2,20 @@ import {Dispatch} from "redux"; import {loggedInUserProfile} from "@/rest/UserAuthProviderApplication"; import {setProfile} from "@/state/profileSlice"; import {dispatchError} from "@/utils/DispatchFunctions"; +import {useSelector} from "react-redux"; +import {RootState} from "@/state/store"; +import {AxiosError} from "axios"; +import {ACCOUNT_INACTIVE_ERROR_CODE, PROFILE_NOT_FOUND_CODE} from "@/constants/ServiceConstants"; -export const addUserProfile = (dispatch: Dispatch) => { - loggedInUserProfile() - .then(response => { - dispatch(setProfile(response)); - }).catch(error => { - dispatchError(dispatch, error); - }) +export const addUserProfile = (dispatch: Dispatch, router: any, authorities: any) => { + loggedInUserProfile() + .then(response => { + dispatch(setProfile(response)); + }).catch((error: any) => { + if (error?.response?.data?.code === PROFILE_NOT_FOUND_CODE && authorities?.includes('ROLE_DUMMY')) { + router.push("/profile-addition-initial"); + } else { + dispatchError(dispatch, error); + } + }) } \ No newline at end of file