feat: token generate with google logina dded
This commit is contained in:
@@ -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',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
import { Button } from '@mui/material';
|
import { Button } from '@mui/material';
|
||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import type { GoogleCodeClientResponse } from '../../types/userTypes';
|
import type {
|
||||||
|
GoogleCodeClientResponse,
|
||||||
|
LoginOrSignUpWithGoogleRequest,
|
||||||
|
} from '../../types/userTypes';
|
||||||
import { loginOrSignUpWithGoogle } from '../../api/authorizationAPI';
|
import { loginOrSignUpWithGoogle } from '../../api/authorizationAPI';
|
||||||
import type { GUID } from '@/types/commonTypes';
|
import type { GUID } from '@/types/commonTypes';
|
||||||
import { Google } from 'iconsax-react';
|
import { Google } from 'iconsax-react';
|
||||||
import { Icon, useToast } from '@rkheftan/harmony-ui';
|
import { Icon, useToast } from '@rkheftan/harmony-ui';
|
||||||
import { useApi } from '@/hooks/useApi';
|
import { useApi } from '@/hooks/useApi';
|
||||||
|
import { generateTokenWithGoogle } from '../../api/identityAPI';
|
||||||
|
import { useAuth } from '@/hooks/useAuth';
|
||||||
|
|
||||||
export interface GoogleAuthenticationProps {
|
export interface GoogleAuthenticationProps {
|
||||||
disabled: boolean;
|
disabled: boolean;
|
||||||
@@ -24,6 +29,7 @@ export const GoogleAuthentication = ({
|
|||||||
useApi(loginOrSignUpWithGoogle);
|
useApi(loginOrSignUpWithGoogle);
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
const clientRef = useRef<any>(null);
|
const clientRef = useRef<any>(null);
|
||||||
|
const auth = useAuth();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const script = document.createElement('script');
|
const script = document.createElement('script');
|
||||||
@@ -39,14 +45,20 @@ export const GoogleAuthentication = ({
|
|||||||
ux_mode: 'popup',
|
ux_mode: 'popup',
|
||||||
response_type: 'code',
|
response_type: 'code',
|
||||||
callback: async (resp: GoogleCodeClientResponse) => {
|
callback: async (resp: GoogleCodeClientResponse) => {
|
||||||
const res = await loginWithGoogleCall({
|
const apiRequest: LoginOrSignUpWithGoogleRequest = {
|
||||||
idToken: resp.id_token,
|
idToken: resp.id_token,
|
||||||
returnUrl: authReturnUrl,
|
returnUrl: authReturnUrl,
|
||||||
});
|
};
|
||||||
|
const res = await loginWithGoogleCall(apiRequest);
|
||||||
|
|
||||||
if (!res) return;
|
if (!res) return;
|
||||||
|
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
|
const tokenRes = await generateTokenWithGoogle(apiRequest);
|
||||||
|
auth.login({
|
||||||
|
...tokenRes.data,
|
||||||
|
});
|
||||||
|
|
||||||
onGoogleAuthenticated(res.userId);
|
onGoogleAuthenticated(res.userId);
|
||||||
} else {
|
} else {
|
||||||
toast({
|
toast({
|
||||||
|
|||||||
Reference in New Issue
Block a user