{"version":3,"file":"OneTapProvider.mjs","sources":["../../../../../../../web/src/oneTap/OneTapProvider.ts"],"sourcesContent":["import { GOOGLE_ONE_TAP_SCRIPT_URL, loadESModule, OneTapStartResponse, RPCManifest } from '@stytch/core';\nimport { StytchAPIError } from '@stytch/core/public';\nimport type { CredentialResponse } from 'google-one-tap';\n\nimport { GoogleOneTapClient } from './GoogleOneTapClient';\n\nexport const loadGoogleOneTapClient = (): Promise<google.accounts['id']> =>\n  loadESModule(GOOGLE_ONE_TAP_SCRIPT_URL, () => window.google.accounts.id);\n\ntype OneTapNotLoadedReason =\n  // These come from the API directly - err.error_type\n  | 'oauth_config_not_found'\n  | 'no_login_redirect_urls_set'\n  | 'no_signup_redirect_urls_set'\n  // If we have an unhandled error :$\n  | string;\n\ntype OneTapLoadResult =\n  | { success: true; client: GoogleOneTapClient }\n  | { success: false; reason: OneTapNotLoadedReason };\n\n/**\n * Handles loading One Tap config from Stytch API and initializing {@link GoogleOneTapClient}\n */\nexport class OneTapProvider {\n  private googleConfig?: Promise<OneTapStartResponse>;\n\n  constructor(\n    private _publicToken: string,\n    private clientsideServices: RPCManifest,\n  ) {}\n\n  async createOneTapClient(): Promise<OneTapLoadResult> {\n    let googleClientId: string;\n    try {\n      ({ googleClientId } = await this.fetchGoogleStart());\n    } catch (e) {\n      const err = StytchAPIError.from(e);\n      return { success: false, reason: err.error_type };\n    }\n\n    if (googleClientId === '') {\n      return { success: false, reason: 'oauth_config_not_found' };\n    }\n    const client = new GoogleOneTapClient(await loadGoogleOneTapClient(), googleClientId);\n    return { success: true, client };\n  }\n\n  createOnSuccessHandler =\n    ({\n      loginRedirectUrl,\n      signupRedirectUrl,\n      onSuccess,\n    }: {\n      loginRedirectUrl?: string;\n      signupRedirectUrl?: string;\n      onSuccess: (redirect_url: string) => void;\n    }) =>\n    async (response: CredentialResponse) => {\n      const { credential } = response;\n      const { redirect_url } = await this.submitGoogleOneTapToken({\n        credential,\n        loginRedirectUrl,\n        signupRedirectUrl,\n      });\n      onSuccess(redirect_url);\n    };\n\n  private fetchGoogleStart() {\n    if (this.googleConfig) {\n      return this.googleConfig;\n    }\n    this.googleConfig = this.clientsideServices.oneTapStart({\n      publicToken: this._publicToken,\n    });\n    return this.googleConfig;\n  }\n\n  private async submitGoogleOneTapToken({\n    credential,\n    loginRedirectUrl,\n    signupRedirectUrl,\n  }: {\n    credential: string;\n    loginRedirectUrl?: string;\n    signupRedirectUrl?: string;\n  }) {\n    const { oauthCallbackId } = await this.fetchGoogleStart();\n\n    return this.clientsideServices.oneTapSubmit({\n      publicToken: this._publicToken,\n      idToken: credential,\n      loginRedirectURL: loginRedirectUrl,\n      oauthCallbackID: oauthCallbackId,\n      signupRedirectURL: signupRedirectUrl,\n    });\n  }\n\n  redirectOnSuccess = (redirect_url: string) => {\n    window.location.href = redirect_url;\n  };\n\n  /**\n   * Google One Tap will show a banner on the bottom of the screen on certain mobile devices\n   * This logic is controlled via some sniffing of the useragent string on startup\n   * These specific strings were extracted from the One Tap minified source code\n   * See the linked PR for details and screenshots\n   */\n  static willGoogleOneTapShowEmbedded(ua = navigator.userAgent): boolean {\n    const uaContains = (userAgent: string, searchString: string) => userAgent.indexOf(searchString) !== -1;\n\n    const isTabletUA =\n      uaContains(ua, 'iPad') || (uaContains(ua, 'Android') && !uaContains(ua, 'Mobile')) || uaContains(ua, 'Silk');\n\n    const isMobileUA =\n      uaContains(ua, 'iPod') || uaContains(ua, 'iPhone') || uaContains(ua, 'Android') || uaContains(ua, 'IEMobile');\n\n    return !isTabletUA && isMobileUA;\n  }\n}\n"],"names":["loadGoogleOneTapClient","loadESModule","GOOGLE_ONE_TAP_SCRIPT_URL","window","google","accounts","id","OneTapProvider","googleConfig","clientsideServices","_publicToken","createOneTapClient","googleClientId","fetchGoogleStart","e","err","StytchAPIError","from","success","reason","error_type","client","GoogleOneTapClient","createOnSuccessHandler","loginRedirectUrl","signupRedirectUrl","onSuccess","response","credential","redirect_url","submitGoogleOneTapToken","oneTapStart","publicToken","oauthCallbackId","oneTapSubmit","idToken","loginRedirectURL","oauthCallbackID","signupRedirectURL","redirectOnSuccess","location","href","willGoogleOneTapShowEmbedded","ua","navigator","userAgent","uaContains","searchString","indexOf","isTabletUA","isMobileUA"],"mappings":";;;;;AAMO,MAAMA,sBAAAA,GAAyB,IACpCC,YAAAA,CAAaC,yBAAAA,EAA2B,IAAMC,MAAAA,CAAOC,MAAM,CAACC,QAAQ,CAACC,EAAE;AAczE;;AAEC,IACM,MAAMC,cAAAA,CAAAA;;;IACHC,YAAAA;AAER,IAAA,WAAA,CACE,YAA4B,EACpBC,kBAA+B,CACvC;aAFQC,YAAAA,GAAAA,YAAAA;aACAD,kBAAAA,GAAAA,kBAAAA;AACP,IAAA;AAEH,IAAA,MAAME,kBAAAA,GAAgD;QACpD,IAAIC,cAAAA;QACJ,IAAI;YACD,CAAA,EAAEA,cAAc,EAAE,GAAG,MAAM,IAAI,CAACC,gBAAgB,EAAC;AACpD,QAAA,CAAA,CAAE,OAAOC,CAAAA,EAAG;YACV,MAAMC,GAAAA,GAAMC,cAAAA,CAAeC,IAAI,CAACH,CAAAA,CAAAA;YAChC,OAAO;gBAAEI,OAAAA,EAAS,KAAA;AAAOC,gBAAAA,MAAAA,EAAQJ,IAAIK;AAAW,aAAA;AAClD,QAAA;AAEA,QAAA,IAAIR,mBAAmB,EAAA,EAAI;YACzB,OAAO;gBAAEM,OAAAA,EAAS,KAAA;gBAAOC,MAAAA,EAAQ;AAAyB,aAAA;AAC5D,QAAA;AACA,QAAA,MAAME,MAAAA,GAAS,IAAIC,kBAAAA,CAAmB,MAAMtB,sBAAAA,EAAAA,EAA0BY,cAAAA,CAAAA;QACtE,OAAO;YAAEM,OAAAA,EAAS,IAAA;AAAMG,YAAAA;AAAO,SAAA;AACjC,IAAA;IAEAE,sBAAAA,GACE,CAAC,EACCC,gBAAgB,EAChBC,iBAAiB,EACjBC,SAAS,EAKV,GACD,OAAOC,QAAAA,GAAAA;YACL,MAAM,EAAEC,UAAU,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