Skip to content

Commit 7381c45

Browse files
authored
Fix google error (#483)
1 parent 67432a5 commit 7381c45

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

admin_app/src/app/login/page.tsx

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ const Login = () => {
4343
const [isLoading, setIsLoading] = React.useState(true);
4444
const { login, loginGoogle, loginError } = useAuth();
4545
const [recoveryCodes, setRecoveryCodes] = React.useState<string[]>([]);
46+
const [isRendered, setIsRendered] = React.useState<boolean>(false);
47+
const signinDiv = React.useCallback((node: HTMLDivElement | null) => {
48+
if (node !== null) {
49+
setIsRendered(true);
50+
}
51+
}, []);
52+
4653
const iconStyles = {
4754
color: appColors.white,
4855
width: { xs: "30%", lg: "40%" },
@@ -63,37 +70,39 @@ const Login = () => {
6370
};
6471

6572
useEffect(() => {
66-
const fetchRegisterPrompt = async () => {
67-
const data = await getRegisterOption();
68-
setShowAdminAlertModal(data.require_register);
69-
setIsLoading(false);
70-
};
71-
fetchRegisterPrompt();
7273
const handleCredentialResponse = (response: any) => {
7374
loginGoogle({
7475
client_id: response.client_id,
7576
credential: response.credential,
7677
});
7778
};
78-
window.google.accounts.id.initialize({
79-
client_id: NEXT_PUBLIC_GOOGLE_LOGIN_CLIENT_ID,
80-
callback: (data) => handleCredentialResponse(data),
81-
state_cookie_domain: "https://example.com",
82-
});
83-
84-
const signinDiv = document.getElementById("signinDiv");
85-
86-
if (signinDiv) {
87-
window.google.accounts.id.renderButton(signinDiv, {
88-
type: "standard",
89-
shape: "pill",
90-
theme: "outline",
91-
size: "large",
92-
width: 275,
79+
if (isRendered) {
80+
window.google.accounts.id.initialize({
81+
client_id: NEXT_PUBLIC_GOOGLE_LOGIN_CLIENT_ID,
82+
callback: (data) => handleCredentialResponse(data),
83+
state_cookie_domain: "https://example.com",
9384
});
85+
const signinDivId = document.getElementById("signinDiv");
86+
if (signinDivId) {
87+
window.google.accounts.id.renderButton(signinDivId, {
88+
type: "standard",
89+
shape: "pill",
90+
theme: "outline",
91+
size: "large",
92+
width: 275,
93+
});
94+
}
9495
}
95-
}, []);
96+
}, [isRendered]);
9697

98+
useEffect(() => {
99+
const fetchRegisterPrompt = async () => {
100+
const data = await getRegisterOption();
101+
setShowAdminAlertModal(data.require_register);
102+
setIsLoading(false);
103+
};
104+
fetchRegisterPrompt();
105+
}, []);
97106
useEffect(() => {
98107
if (recoveryCodes.length > 0) {
99108
setShowConfirmationModal(true);
@@ -348,7 +357,7 @@ const Login = () => {
348357
alignItems="center"
349358
justifyContent="center"
350359
>
351-
<div id="signinDiv" />
360+
<div ref={signinDiv} id="signinDiv" />
352361
<Layout.Spacer multiplier={2.5} />
353362
<Box display="flex" alignItems="center" width="100%">
354363
<Box flexGrow={1} height="1px" bgcolor="lightgrey" />
@@ -360,6 +369,7 @@ const Login = () => {
360369
<Layout.Spacer multiplier={1.5} />
361370
</Box>
362371
)}
372+
363373
<Box
364374
component="form"
365375
noValidate

0 commit comments

Comments
 (0)