From ef47214176303c6cd9b230dd9f218e3fa493df81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=85=D9=87=D8=B1=D8=B2=D8=A7=D8=AF=20=D9=82=D8=AF=D8=B1?= =?UTF-8?q?=D8=AA=DB=8C?= Date: Sun, 17 Aug 2025 16:45:29 +0330 Subject: [PATCH] feat: token generate with google logina dded --- src/features/authorization/api/identityAPI.ts | 22 +++++++++++++++++++ .../GoogleAuthentication.tsx | 18 ++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/features/authorization/api/identityAPI.ts b/src/features/authorization/api/identityAPI.ts index 9951d22..028cfd6 100644 --- a/src/features/authorization/api/identityAPI.ts +++ b/src/features/authorization/api/identityAPI.ts @@ -59,3 +59,25 @@ export const generateTokenWithOtp = (request: GenerateTokenWithOTP) => { }, ); }; + +export interface GenerateTokenWithGoogle { + idToken: string; +} + +export const generateTokenWithGoogle = (request: GenerateTokenWithGoogle) => { + const body = new URLSearchParams(); + body.set('grant_type', 'google'); + body.set('client_id', import.meta.env.VITE_IDENTITY_CLIENT_ID); + body.set('scope', import.meta.env.VITE_IDENTITY_SCOPE); + body.set('idtoken', request.idToken); + + return apiClient.post( + import.meta.env.VITE_IDENTITY_URL, + body.toString(), + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }, + ); +}; diff --git a/src/features/authorization/components/AuthenticationSteps/GoogleAuthentication.tsx b/src/features/authorization/components/AuthenticationSteps/GoogleAuthentication.tsx index d517362..bf3eaea 100644 --- a/src/features/authorization/components/AuthenticationSteps/GoogleAuthentication.tsx +++ b/src/features/authorization/components/AuthenticationSteps/GoogleAuthentication.tsx @@ -1,12 +1,17 @@ import { Button } from '@mui/material'; import { useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; -import type { GoogleCodeClientResponse } from '../../types/userTypes'; +import type { + GoogleCodeClientResponse, + LoginOrSignUpWithGoogleRequest, +} from '../../types/userTypes'; import { loginOrSignUpWithGoogle } from '../../api/authorizationAPI'; import type { GUID } from '@/types/commonTypes'; import { Google } from 'iconsax-react'; import { Icon, useToast } from '@rkheftan/harmony-ui'; import { useApi } from '@/hooks/useApi'; +import { generateTokenWithGoogle } from '../../api/identityAPI'; +import { useAuth } from '@/hooks/useAuth'; export interface GoogleAuthenticationProps { disabled: boolean; @@ -24,6 +29,7 @@ export const GoogleAuthentication = ({ useApi(loginOrSignUpWithGoogle); const toast = useToast(); const clientRef = useRef(null); + const auth = useAuth(); useEffect(() => { const script = document.createElement('script'); @@ -39,14 +45,20 @@ export const GoogleAuthentication = ({ ux_mode: 'popup', response_type: 'code', callback: async (resp: GoogleCodeClientResponse) => { - const res = await loginWithGoogleCall({ + const apiRequest: LoginOrSignUpWithGoogleRequest = { idToken: resp.id_token, returnUrl: authReturnUrl, - }); + }; + const res = await loginWithGoogleCall(apiRequest); if (!res) return; if (res.success) { + const tokenRes = await generateTokenWithGoogle(apiRequest); + auth.login({ + ...tokenRes.data, + }); + onGoogleAuthenticated(res.userId); } else { toast({