From f76ef3b5c75a936d97c28b994adf607a308cbf08 Mon Sep 17 00:00:00 2001 From: Prakhargarg-2010196 <23prakhargarg2002@gmail.com> Date: Mon, 14 Oct 2024 15:33:06 +0530 Subject: [PATCH 1/4] feat(platform):Create ui link for resend otp --- apps/platform/src/app/auth/otp/page.tsx | 46 ++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/apps/platform/src/app/auth/otp/page.tsx b/apps/platform/src/app/auth/otp/page.tsx index d6145e9a..b9c03d14 100644 --- a/apps/platform/src/app/auth/otp/page.tsx +++ b/apps/platform/src/app/auth/otp/page.tsx @@ -26,6 +26,7 @@ export default function AuthOTPPage(): React.JSX.Element { const [otp, setOtp] = useState('') const [isLoading, setIsLoading] = useState(false) const [isInvalidOtp, setIsInvalidOtp] = useState(false) + const [isLoadingRefresh, setIsLoadingRefresh] = useState(false) const router = useRouter() @@ -88,7 +89,28 @@ export default function AuthOTPPage(): React.JSX.Element { } } } - + const handleResendOtp = async (userEmail: string): Promise => { + try { + setIsLoadingRefresh(true) + const response = await fetch( + `${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/resend-otp/${encodeURIComponent(userEmail)}`, + { + method: 'POST' + } + ) + if (response.status === 429) { + toast.error("Couldn't send OTP, too many requests") + setIsLoadingRefresh(false) + } else if (response.ok) + toast.success('OTP successfully sent to your email') + setIsLoadingRefresh(false) + } catch (error) { + // eslint-disable-next-line no-console -- we need to log the error + console.error(`Failed to send OTP: ${error}`) + toast.error("Couldn't send OTP .") + setIsLoadingRefresh(false) + } + } return (
@@ -144,6 +166,28 @@ export default function AuthOTPPage(): React.JSX.Element { > {isLoading ? : 'Verify'} +
+ Didn’t receive OTP? + + {isLoadingRefresh ? ( + + + + ) : ( + + )} + +
From 61c25dd3bfc84a7a35c8069f039ca7b6c54dbd76 Mon Sep 17 00:00:00 2001 From: Prakhargarg-2010196 <23prakhargarg2002@gmail.com> Date: Mon, 28 Oct 2024 16:20:45 +0530 Subject: [PATCH 2/4] refactor(auth): Refactor API client for otp resend - Created new controller instance for better management of API interactions. - Added authentication controller in API client for handling OTP-related requests. - Introduced type definitions for authentication to improve type safety. - Renamed index types definition for consistency. --- apps/platform/src/app/auth/otp/page.tsx | 29 ++++++++++------- apps/platform/src/lib/controller-instance.ts | 31 +++++++++++++++++++ packages/api-client/src/controllers/auth.ts | 26 ++++++++++++++++ packages/api-client/src/index.ts | 5 +-- packages/api-client/src/types/auth.types.ts | 4 +++ .../{index.types.d.ts => index.types.ts} | 0 6 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 apps/platform/src/lib/controller-instance.ts create mode 100644 packages/api-client/src/controllers/auth.ts create mode 100644 packages/api-client/src/types/auth.types.ts rename packages/api-client/src/types/{index.types.d.ts => index.types.ts} (100%) diff --git a/apps/platform/src/app/auth/otp/page.tsx b/apps/platform/src/app/auth/otp/page.tsx index b9c03d14..cfd94075 100644 --- a/apps/platform/src/app/auth/otp/page.tsx +++ b/apps/platform/src/app/auth/otp/page.tsx @@ -8,6 +8,8 @@ import Cookies from 'js-cookie' import { toast } from 'sonner' import { LoadingSVG } from '@public/svg/shared' import { KeyshadeBigSVG } from '@public/svg/auth' +import type { ClientResponse } from '@keyshade/api-client/dist/src/types/index.types' +import type { ResendOTPRequest } from '@keyshade/api-client/dist/src/types/auth.types' import { GeistSansFont } from '@/fonts' import { Button } from '@/components/ui/button' import { @@ -19,6 +21,7 @@ import { import { authEmailAtom } from '@/store' import type { User } from '@/types' import { zUser } from '@/types' +import ControllerInstance from '@/lib/controller-instance' export default function AuthOTPPage(): React.JSX.Element { const email = useAtomValue(authEmailAtom) @@ -92,18 +95,22 @@ export default function AuthOTPPage(): React.JSX.Element { const handleResendOtp = async (userEmail: string): Promise => { try { setIsLoadingRefresh(true) - const response = await fetch( - `${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/resend-otp/${encodeURIComponent(userEmail)}`, - { - method: 'POST' - } - ) - if (response.status === 429) { + //TODO:Remove this once initialisation is fixed + // ControllerInstance.initialize('http://localhost:4200') + + const { error, success } = + (await ControllerInstance.getInstance().authController.resendOTP({ + userEmail: encodeURIComponent(userEmail) + })) as ClientResponse + if (success) { + toast.success('OTP successfully sent to your email') + setIsLoadingRefresh(false) + } else { + // eslint-disable-next-line no-console -- we need to log the error + console.log(error) toast.error("Couldn't send OTP, too many requests") setIsLoadingRefresh(false) - } else if (response.ok) - toast.success('OTP successfully sent to your email') - setIsLoadingRefresh(false) + } } catch (error) { // eslint-disable-next-line no-console -- we need to log the error console.error(`Failed to send OTP: ${error}`) @@ -171,7 +178,7 @@ export default function AuthOTPPage(): React.JSX.Element { {isLoadingRefresh ? ( - + ) : (