google-one-tap.ts 896 B

123456789101112131415161718192021222324252627
  1. import { CredentialResponse, IdConfiguration } from 'google-one-tap';
  2. export default function googleOneTap(
  3. { client_id, auto_select = false, cancel_on_tap_outside = false, context = 'signin' }: IdConfiguration,
  4. callback: (response: CredentialResponse) => void,
  5. otherOptions?: Omit<IdConfiguration, 'client_id'>,
  6. ) {
  7. const contextValue = ['signin', 'signup', 'use'].includes(context) ? context : 'signin';
  8. if (!client_id) {
  9. throw new Error('client_id is required');
  10. }
  11. if (typeof window !== 'undefined' && window.document) {
  12. try {
  13. window.google.accounts.id.initialize({
  14. client_id: client_id,
  15. callback: callback,
  16. auto_select: auto_select,
  17. cancel_on_tap_outside: cancel_on_tap_outside,
  18. context: contextValue,
  19. ...otherOptions,
  20. });
  21. window.google.accounts.id.prompt();
  22. } catch {
  23. /* empty */
  24. }
  25. }
  26. }