From bf956fe18cbb839b0d81b45ba63050fb7f8bfd71 Mon Sep 17 00:00:00 2001 From: Amruth Pillai Date: Mon, 22 Aug 2022 11:26:30 +0200 Subject: [PATCH] use @react-oauth/google library for google auth --- client/modals/auth/LoginModal.tsx | 43 +++------ client/modals/auth/RegisterModal.tsx | 34 +++----- client/package.json | 119 ++++++++++++------------- client/pages/_app.tsx | 35 ++++---- client/public/locales/en/modals.json | 3 +- pnpm-lock.yaml | 125 +++++++++++++++------------ server/package.json | 2 +- 7 files changed, 173 insertions(+), 188 deletions(-) diff --git a/client/modals/auth/LoginModal.tsx b/client/modals/auth/LoginModal.tsx index 33b6c6c0..bf7b8f7d 100644 --- a/client/modals/auth/LoginModal.tsx +++ b/client/modals/auth/LoginModal.tsx @@ -1,7 +1,8 @@ import env from '@beam-australia/react-env'; import { joiResolver } from '@hookform/resolvers/joi'; -import { Google, Login, Visibility, VisibilityOff } from '@mui/icons-material'; +import { Login, Visibility, VisibilityOff } from '@mui/icons-material'; import { Button, IconButton, InputAdornment, TextField } from '@mui/material'; +import { CredentialResponse, GoogleLogin } from '@react-oauth/google'; import Joi from 'joi'; import { isEmpty } from 'lodash'; import { Trans, useTranslation } from 'next-i18next'; @@ -17,8 +18,6 @@ import { ServerError } from '@/services/axios'; import { useAppDispatch, useAppSelector } from '@/store/hooks'; import { setModalState } from '@/store/modal/modalSlice'; -declare const google: any; - type FormData = { identifier: string; password: string; @@ -85,28 +84,16 @@ const LoginModal: React.FC = () => { dispatch(setModalState({ modal: 'auth.forgot', state: { open: true } })); }; - const handleLoginWithGoogle = async () => { - console.log(process.env.PUBLIC_GOOGLE_CLIENT_ID, env('GOOGLE_CLIENT_ID')); + const handleLoginWithGoogle = async (response: CredentialResponse) => { + if (response.credential) { + await loginWithGoogleMutation({ credential: response.credential }, { onError: handleLoginWithGoogleError }); - google.accounts.id.initialize({ - auto_select: true, - itp_support: true, - client_id: env('GOOGLE_CLIENT_ID'), - callback: async (response: any) => { - await loginWithGoogleMutation({ credential: response.credential }); + handleClose(); + } + }; - handleClose(); - }, - }); - - google.accounts.id.prompt((notification: any) => { - if (notification.isNotDisplayed() || notification.isSkippedMoment()) { - const reason = notification.getNotDisplayedReason() || notification.getSkippedReason(); - - toast.error(`Google returned an error while trying to sign in: ${reason}.`); - toast("Please try logging in using email/password, or use another browser that supports Google's One Tap API."); - } - }); + const handleLoginWithGoogleError = () => { + toast("Please try logging in using email/password, or use another browser that supports Google's One Tap API."); }; const PasswordVisibility = (): React.ReactElement => { @@ -130,15 +117,7 @@ const LoginModal: React.FC = () => { footerChildren={
{!isEmpty(env('GOOGLE_CLIENT_ID')) && ( - + )} + )}