feat: token generate with google logina dded

This commit is contained in:
مهرزاد قدرتی
2025-08-17 16:45:29 +03:30
parent 983c25ad94
commit ef47214176
2 changed files with 37 additions and 3 deletions

View File

@@ -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<GenerateTokenResponse>(
import.meta.env.VITE_IDENTITY_URL,
body.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
},
);
};

View File

@@ -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<any>(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({