use @react-oauth/google library for google auth

This commit is contained in:
Amruth Pillai
2022-08-22 11:26:30 +02:00
parent 4114f1e1dd
commit bf956fe18c
7 changed files with 173 additions and 188 deletions

View File

@ -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={
<div className="flex gap-4">
{!isEmpty(env('GOOGLE_CLIENT_ID')) && (
<Button
type="submit"
variant="outlined"
disabled={isLoading}
startIcon={<Google />}
onClick={handleLoginWithGoogle}
>
{t<string>('modals.auth.login.actions.google')}
</Button>
<GoogleLogin onSuccess={handleLoginWithGoogle} onError={handleLoginWithGoogleError} />
)}
<Button type="submit" onClick={handleSubmit(onSubmit)} disabled={isLoading}>

View File

@ -1,11 +1,13 @@
import env from '@beam-australia/react-env';
import { joiResolver } from '@hookform/resolvers/joi';
import { Google, HowToReg } from '@mui/icons-material';
import { HowToReg } from '@mui/icons-material';
import { Button, 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';
import { Controller, useForm } from 'react-hook-form';
import toast from 'react-hot-toast';
import { useMutation } from 'react-query';
import BaseModal from '@/components/shared/BaseModal';
@ -14,8 +16,6 @@ import { ServerError } from '@/services/axios';
import { useAppDispatch, useAppSelector } from '@/store/hooks';
import { setModalState } from '@/store/modal/modalSlice';
declare const google: any;
type FormData = {
name: string;
username: string;
@ -79,18 +79,16 @@ const RegisterModal: React.FC = () => {
dispatch(setModalState({ modal: 'auth.login', state: { open: true } }));
};
const handleLoginWithGoogle = async () => {
google.accounts.id.initialize({
client_id: env('GOOGLE_CLIENT_ID'),
callback: async (response: any) => {
await loginWithGoogleMutation({ credential: response.credential });
const handleLoginWithGoogle = async (response: CredentialResponse) => {
if (response.credential) {
await loginWithGoogleMutation({ credential: response.credential }, { onError: handleLoginWithGoogleError });
handleClose();
},
auto_select: false,
});
handleClose();
}
};
google.accounts.id.prompt();
const handleLoginWithGoogleError = () => {
toast("Please try logging in using email/password, or use another browser that supports Google's One Tap API.");
};
return (
@ -102,15 +100,7 @@ const RegisterModal: React.FC = () => {
footerChildren={
<div className="flex gap-4">
{!isEmpty(env('GOOGLE_CLIENT_ID')) && (
<Button
type="submit"
variant="outlined"
disabled={isLoading}
startIcon={<Google />}
onClick={handleLoginWithGoogle}
>
{t<string>('modals.auth.register.actions.google')}
</Button>
<GoogleLogin onSuccess={handleLoginWithGoogle} onError={handleLoginWithGoogleError} />
)}
<Button type="submit" onClick={handleSubmit(onSubmit)} disabled={isLoading}>

View File

@ -8,78 +8,79 @@
"sitemap": "next-sitemap --config next-sitemap.config.js"
},
"dependencies": {
"joi": "^17.6.0",
"clsx": "^1.2.1",
"next": "12.2.5",
"uuid": "^8.3.2",
"axios": "^0.27.2",
"dayjs": "^1.11.5",
"react": "18.2.0",
"redux": "^4.2.0",
"sharp": "^0.30.7",
"lodash": "^4.17.21",
"nanoid": "^3.3.4",
"md5-hex": "^4.0.0",
"@mui/lab": "^5.0.0-alpha.95",
"@next/env": "^12.2.5",
"react-dnd": "16.0.1",
"react-dom": "18.2.0",
"downloadjs": "^1.4.7",
"redux-saga": "^1.2.1",
"remark-gfm": "^3.0.1",
"@mui/system": "^5.10.1",
"react-icons": "^4.4.0",
"react-query": "^3.39.2",
"react-redux": "^8.0.2",
"@emotion/css": "^11.10.0",
"next-i18next": "^11.3.0",
"@mui/material": "^5.10.1",
"monaco-editor": "^0.34.0",
"redux-persist": "^6.0.0",
"webfontloader": "^1.6.28",
"@beam-australia/react-env": "^3.1.1",
"@date-io/dayjs": "^2.15.0",
"@emotion/css": "^11.10.0",
"@emotion/react": "^11.10.0",
"react-colorful": "^5.6.1",
"react-markdown": "^8.0.3",
"@emotion/styled": "^11.10.0",
"@hookform/resolvers": "2.9.7",
"@monaco-editor/react": "^4.4.5",
"@mui/icons-material": "^5.8.4",
"@mui/lab": "^5.0.0-alpha.95",
"@mui/material": "^5.10.1",
"@mui/system": "^5.10.1",
"@mui/x-date-pickers": "5.0.0-beta.6",
"@next/env": "^12.2.5",
"@react-oauth/google": "^0.2.6",
"@reduxjs/toolkit": "^1.8.5",
"axios": "^0.27.2",
"clsx": "^1.2.1",
"dayjs": "^1.11.5",
"downloadjs": "^1.4.7",
"joi": "^17.6.0",
"lodash": "^4.17.21",
"md5-hex": "^4.0.0",
"monaco-editor": "^0.34.0",
"nanoid": "^3.3.4",
"next": "12.2.5",
"next-i18next": "^12.0.0",
"react": "18",
"react-beautiful-dnd": "^13.1.0",
"react-colorful": "^5.6.1",
"react-dnd": "16.0.1",
"react-dnd-html5-backend": "16.0.1",
"react-dom": "18",
"react-hook-form": "^7.34.2",
"react-hot-toast": "2.3.0",
"@reduxjs/toolkit": "^1.8.5",
"react-hotkeys-hook": "^3.4.7",
"@hookform/resolvers": "2.9.7",
"@mui/icons-material": "^5.8.4",
"@mui/x-date-pickers": "5.0.0-beta.6",
"react-beautiful-dnd": "^13.1.0",
"@monaco-editor/react": "^4.4.5",
"react-icons": "^4.4.0",
"react-markdown": "^8.0.3",
"react-query": "^3.39.2",
"react-redux": "^8.0.2",
"react-zoom-pan-pinch": "^2.1.3",
"react-dnd-html5-backend": "16.0.1",
"@beam-australia/react-env": "^3.1.1"
"redux": "^4.2.0",
"redux-persist": "^6.0.0",
"redux-saga": "^1.2.1",
"remark-gfm": "^3.0.1",
"sharp": "^0.30.7",
"uuid": "^8.3.2",
"webfontloader": "^1.6.28"
},
"resolutions": {
"@types/react": "18.0.17",
"@types/react-dom": "18.0.6"
"@types/react": "18",
"@types/react-dom": "18"
},
"devDependencies": {
"sass": "^1.54.5",
"csstype": "^3.1.0",
"postcss": "^8.4.16",
"typescript": "^4.7.4",
"@babel/core": "^7.18.10",
"@types/node": "18.7.8",
"@types/uuid": "^8.3.4",
"tailwindcss": "^3.1.8",
"@types/react": "18.0.17",
"autoprefixer": "^10.4.8",
"next-sitemap": "^3.1.21",
"@types/lodash": "^4.14.184",
"@types/react-dom": "18.0.6",
"@types/downloadjs": "^1.4.3",
"@types/react-redux": "^7.1.24",
"@types/tailwindcss": "^3.0.11",
"eslint-config-next": "12.2.5",
"@types/webfontloader": "^1.6.34",
"@reactive-resume/schema": "workspace:*",
"@tailwindcss/typography": "^0.5.4",
"@types/react-beautiful-dnd": "^13.1.2"
"@types/downloadjs": "^1.4.3",
"@types/lodash": "^4.14.184",
"@types/node": "18.7.9",
"@types/react": "18",
"@types/react-beautiful-dnd": "^13.1.2",
"@types/react-dom": "18",
"@types/react-redux": "^7.1.24",
"@types/tailwindcss": "^3.0.11",
"@types/uuid": "^8.3.4",
"@types/webfontloader": "^1.6.34",
"autoprefixer": "^10.4.8",
"csstype": "^3.1.0",
"eslint-config-next": "12.2.5",
"next-sitemap": "^3.1.21",
"postcss": "^8.4.16",
"sass": "^1.54.5",
"tailwindcss": "^3.1.8",
"typescript": "^4.7.4"
}
}

View File

@ -1,10 +1,11 @@
import '@/styles/globals.scss';
import env from '@beam-australia/react-env';
import DayjsAdapter from '@date-io/dayjs';
import { LocalizationProvider } from '@mui/x-date-pickers';
import { GoogleOAuthProvider } from '@react-oauth/google';
import type { AppProps } from 'next/app';
import Head from 'next/head';
import Script from 'next/script';
import { appWithTranslation } from 'next-i18next';
import { Toaster } from 'react-hot-toast';
import { QueryClientProvider } from 'react-query';
@ -34,27 +35,27 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
<ReduxProvider store={store}>
<LocalizationProvider dateAdapter={DayjsAdapter}>
<PersistGate loading={null} persistor={persistor}>
<QueryClientProvider client={queryClient}>
<WrapperRegistry>
<Loading />
<GoogleOAuthProvider clientId={env('GOOGLE_CLIENT_ID')}>
<QueryClientProvider client={queryClient}>
<WrapperRegistry>
<Loading />
<Component {...pageProps} />
<Component {...pageProps} />
<ModalWrapper />
<Toaster
position="bottom-right"
toastOptions={{
duration: 4000,
className: 'toast',
}}
/>
</WrapperRegistry>
</QueryClientProvider>
<ModalWrapper />
<Toaster
position="bottom-right"
toastOptions={{
duration: 4000,
className: 'toast',
}}
/>
</WrapperRegistry>
</QueryClientProvider>
</GoogleOAuthProvider>
</PersistGate>
</LocalizationProvider>
</ReduxProvider>
<Script src="https://accounts.google.com/gsi/client" async defer />
</>
);
};

View File

@ -15,8 +15,7 @@
},
"login": {
"actions": {
"login": "Login",
"google": "Login with Google"
"login": "Login"
},
"body": "Please enter your username and password associated with your account to login and access, manage and share your resumes.",
"form": {