Skip to content

Commit 2b50c75

Browse files
committed
Change the Google login button to not show on the login page if Google Client ID is empty and validate the input, show error if invalid client id is supplied. #37
1 parent b5d2f96 commit 2b50c75

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

web/src/components/GoogleLoginButton.jsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {useState} from 'react'
22
import { useGoogleLogin } from '@react-oauth/google';
33
import { useNavigate } from 'react-router-dom';
4-
import { Button } from 'flowbite-react';
4+
import { Button, Popover } from 'flowbite-react';
55
import AuthService from '../services/auth.service';
66
import { Spinner } from 'flowbite-react';
77

@@ -29,20 +29,40 @@ function GoogleLoginButton(props) {
2929
}
3030
});
3131

32+
const googleButton = (
33+
<Button disabled={props.error} color="light" onClick={() => (handleLoginGoogle(), setLoading(true))}>
34+
<svg version="1.1" className="roboto-medium h-5 w-5 mr-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" xmlnsXlink="http://www.w3.org/1999/xlink">
35+
<path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"></path>
36+
<path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"></path>
37+
<path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"></path>
38+
<path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"></path>
39+
<path fill="none" d="M0 0h48v48H0z"></path>
40+
</svg>
41+
Sign in with Google
42+
{props.error &&
43+
44+
<span>⚠️</span>
45+
}
46+
</Button>
47+
);
48+
3249
return (
3350
<>
3451
{loading ? (
3552
<Button color="light">
3653
<Spinner/>
3754
</Button>
3855
): (
39-
<Button color="light" onClick={() => (handleLoginGoogle(), setLoading(true))}><svg version="1.1" className="roboto-medium h-5 w-5 mr-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" xmlnsXlink="http://www.w3.org/1999/xlink">
40-
<path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"></path>
41-
<path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"></path>
42-
<path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"></path>
43-
<path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"></path>
44-
<path fill="none" d="M0 0h48v48H0z"></path>
45-
</svg>Sign in with Google</Button>
56+
props.error ? (
57+
<Popover placement='top' content={<div className="p-2 text-sm text-gray-500">Invalid Google Client ID.</div>} trigger="hover">
58+
<div>
59+
{googleButton}
60+
</div>
61+
</Popover>
62+
):(
63+
googleButton
64+
)
65+
4666
)}
4767

4868
</>

web/src/pages/Login.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ function Login() {
1414
let navigate = useNavigate();
1515
const toast = useToast(8000);
1616

17+
const isValidGoogleID = /^[0-9]+-[a-z0-9]+\.apps\.googleusercontent\.com$/.test(import.meta.env.VITE_GOOGLE_CLIENT_ID);
18+
1719
const handleLogin = (e) => {
1820
e.preventDefault();
1921
AuthService.login(username, password).then(
@@ -49,7 +51,8 @@ function Login() {
4951
<ul>Password: demo</ul>
5052
</div>
5153
):(
52-
<GoogleLoginButton />
54+
import.meta.env.VITE_GOOGLE_CLIENT_ID &&
55+
<GoogleLoginButton error={!isValidGoogleID}/>
5356
)}
5457

5558
<Card>

0 commit comments

Comments
 (0)