Skip to content

Commit

Permalink
refactor: improve async handling (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
novanish authored Oct 30, 2024
1 parent 3eebe97 commit c022c4e
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions frontend/hooks/use-one-tap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,33 @@ import { GOOGLE_CLIENT_ID } from '@/lib/client_env';
const useOneTapSignin = (options, user: User) => {
const [isLoading, setIsLoading] = useState(false);

const oneTap = () => {
if (isLoading) {
return;
}
const { google } = window;
if (google) {
setIsLoading(true);
google.accounts.id.initialize({
// log_level: 'debug',
client_id: GOOGLE_CLIENT_ID!,
callback: async (response: CredentialResponse) => {
setIsLoading(true);

void signIn('googleonetap', {
const oneTap = async () => {
if (isLoading || !window.google) return;

setIsLoading(true);
window.google.accounts.id.initialize({
client_id: GOOGLE_CLIENT_ID!,
callback: async (response: CredentialResponse) => {
try {
await signIn('googleonetap', {
credential: response.credential,
redirect: true,
...options,
});
} finally {
setIsLoading(false);
},
});
}
},
});

google.accounts.id.prompt();
}
window.google.accounts.id.prompt();
};

useEffect(() => {
if (user) {
return;
}
const timeout = setTimeout(() => oneTap(), 1000);
return () => {
clearTimeout(timeout);
};
if (user) return;

const timeout = setTimeout(oneTap, 1000);
return () => clearTimeout(timeout);
}, [user]);

return { isLoading };
Expand Down

0 comments on commit c022c4e

Please sign in to comment.