我试图让用户点击登录时出现一次点击,当页面加载后立即运行时,下面的代码运行良好,但是如果我尝试使其在单击时运行,则会出现错误[ C0]错误,我没有正在运行的广告阻止插件,我也不知道suppressed_by_user
意味着什么?
[suppressed_by_user
详细说明了错误,但不解释导致错误的原因或解决方法。
docs here
这表示用户之前已经手动关闭了“一键式”提示,这触发了“冷却”功能(如<script src="https://accounts.google.com/gsi/client"></script>
...
const signin = () => {
const handleCredentialResponse = (response: any) => {
const credential = response.credential;
const getDetails = async () => {
const params = new window.URLSearchParams({ credential });
const url = `${process.env.REACT_APP_API_BASE_URL}/google?${params}`;
const response = await fetch(url, { method: "GET" });
const data = await response.json();
setLoggedIn(true);
setState({ ...state, participant: data.participant });
};
getDetails();
};
if (state && state.participant && state.participant.id) {
setLoggedIn(true);
} else {
const client_id = process.env.REACT_APP_GOOGLE_CLIENT_ID;
const callback = handleCredentialResponse;
const auto_select = false;
const cancel_on_tap_outside = false;
google.accounts.id.initialize({ client_id, callback, auto_select, cancel_on_tap_outside });
google.accounts.id.prompt((notification: any) => {
console.log(notification); // rl {g: "display", h: false, j: "suppressed_by_user"}
console.log(notification.getNotDisplayedReason()); // suppressed_by_user
});
}
};
...
<div className="center--main-join" onClick={signin}>Sign in or Join</div>
所示)。
在冷却期间,一键式提示被抑制。