Skip to content

Commit

Permalink
refactor(auth): Refactor API client for otp resend
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Prakhargarg-2010196 authored and rajdip-b committed Oct 30, 2024
1 parent f76ef3b commit 61c25dd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
29 changes: 18 additions & 11 deletions apps/platform/src/app/auth/otp/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -92,18 +95,22 @@ export default function AuthOTPPage(): React.JSX.Element {
const handleResendOtp = async (userEmail: string): Promise<void> => {
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<ResendOTPRequest>
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}`)
Expand Down Expand Up @@ -171,7 +178,7 @@ export default function AuthOTPPage(): React.JSX.Element {
<span>
{isLoadingRefresh ? (
<span>
<LoadingSVG className="w-10 h-10" />
<LoadingSVG className="h-10 w-10" />
</span>
) : (
<Button
Expand Down
31 changes: 31 additions & 0 deletions apps/platform/src/lib/controller-instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { AuthController } from '@keyshade/api-client'

export default class ControllerInstance {
private static instance: ControllerInstance | null

private _authController: AuthController | null = null

get authController(): AuthController {
if (!this._authController) {
throw new Error('ControllerInstance not initialized')
}
return this._authController
}

static initialize(baseUrl: string): void {
if (!ControllerInstance.instance) {
const instance = new ControllerInstance()

instance._authController = new AuthController(baseUrl)

ControllerInstance.instance = instance
}
}

static getInstance(): ControllerInstance {
if (!ControllerInstance.instance) {
throw new Error('ControllerInstance not initialized')
}
return ControllerInstance.instance
}
}
26 changes: 26 additions & 0 deletions packages/api-client/src/controllers/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
ResendOTPResponse,
ResendOTPRequest
} from '@api-client/types/auth.types'
import { APIClient } from '@api-client/core/client'
import { parseResponse } from '@api-client/core/response-parser'
import { ClientResponse } from '@api-client/types/index.types'
export default class AuthController {
private apiClient: APIClient

constructor(private readonly backendURL: string) {
this.apiClient = new APIClient(this.backendURL)
}

async resendOTP(
request: ResendOTPRequest,
headers?: Record<string, string>
): Promise<ClientResponse<ResendOTPResponse>> {
const response = await this.apiClient.post(
`/api/auth/resend-otp/${request.userEmail}`,
request,
headers
)
return await parseResponse<ResendOTPResponse>(response)
}
}
5 changes: 3 additions & 2 deletions packages/api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import VariableController from '@api-client/controllers/variable'
import WorkspaceController from '@api-client/controllers/workspace'
import WorkspaceRoleController from '@api-client/controllers/workspace-role'
import WorkspaceMembershipController from '@api-client/controllers/workspace-membership'

import AuthController from '@api-client/controllers/auth'
export {
EnvironmentController,
SecretController,
Expand All @@ -17,5 +17,6 @@ export {
VariableController,
WorkspaceController,
WorkspaceRoleController,
WorkspaceMembershipController
WorkspaceMembershipController,
AuthController
}
4 changes: 4 additions & 0 deletions packages/api-client/src/types/auth.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ResendOTPRequest {
userEmail: string
}
export interface ResendOTPResponse {}

0 comments on commit 61c25dd

Please sign in to comment.