google-oauth-gsi
Version:
A user-friendly API for GIS SDK, using the new [**Google Identity Services SDK**](https://developers.google.com/identity/gsi/web) 🚀
2 lines (1 loc) • 3.43 kB
JavaScript
var googleOauthGsi=function(n){"use strict";var O=Object.defineProperty;var v=(n,l,d)=>l in n?O(n,l,{enumerable:!0,configurable:!0,writable:!0,value:d}):n[l]=d;var m=(n,l,d)=>(v(n,typeof l!="symbol"?l+"":l,d),d);const l=e=>{var o;return(o=e==null?void 0:e.clientId)!=null?o:e==null?void 0:e.client_id};class d{constructor({clientId:t,onScriptLoadError:o,onScriptLoadSuccess:i}){m(this,"clientId");m(this,"scriptLoadedSuccessfully",!1);m(this,"containerHeightMap",{large:"40",medium:"32",small:"20"});this.clientId=t,!(typeof window>"u")&&this.useLoadGsiScript({onScriptLoadError:o,onScriptLoadSuccess:i})}useLoadGsiScript({onScriptLoadSuccess:t,onScriptLoadError:o}={}){const i=document.createElement("script");i.src="https://accounts.google.com/gsi/client",i.async=!0,i.defer=!0,i.onload=()=>{this.scriptLoadedSuccessfully=!0,t&&t()},i.onerror=()=>{this.scriptLoadedSuccessfully=!1,o&&o()},document.body.appendChild(i)}useGoogleLogin(t){return o=>this.useGoogleLoginPostponed(t)(o)}useGoogleLoginPostponed({flow:t="implicit",scope:o="",onSuccess:i,onError:r,onNonOAuthError:a,overrideScope:f,...g}){var y;if(!this.scriptLoadedSuccessfully)return()=>{};const s=t==="implicit"?"initTokenClient":"initCodeClient",u=(y=window.google)==null?void 0:y.accounts.oauth2[s]({client_id:this.clientId,scope:f?o:`openid profile email ${o}`,callback:c=>{if(r&&c.error)return r(c);i&&i(c)},error_callback:c=>{a&&a(c)},...g});return t==="implicit"?c=>u.requestAccessToken(c):()=>u.requestCode()}useGoogleOneTapLogin(t){return()=>{this.useGoogleOneTapLoginPostponed(t)}}useGoogleOneTapLoginPostponed({onError:t,onSuccess:o,hosted_domain:i,use_fedcm_for_prompt:r,cancel_on_tap_outside:a,promptMomentNotification:f}){var g,s;typeof window>"u"||!this.scriptLoadedSuccessfully||((g=window.google)==null||g.accounts.id.initialize({hosted_domain:i,use_fedcm_for_prompt:r,cancel_on_tap_outside:a,client_id:this.clientId,callback:u=>{if(!(u!=null&&u.credential)&&t)return t();const{credential:h,select_by:G}=u;o({select_by:G,credential:h,clientId:l(u)})}}),(s=window.google)==null||s.accounts.id.prompt(f))}useRenderButton(t){return()=>{this.useRenderButtonPostponed(t)}}useRenderButtonPostponed({onSuccess:t,onError:o,useOneTap:i,promptMomentNotification:r,type:a="standard",theme:f="outline",size:g="large",text:s,shape:u,element:h,logo_alignment:G,width:y,locale:c,click_listener:P,use_fedcm_for_prompt:_,...C}){var p,L,I;!this.scriptLoadedSuccessfully||((p=window.google)==null||p.accounts.id.initialize({client_id:this.clientId,use_fedcm_for_prompt:_,callback:w=>{if(!(w!=null&&w.credential)&&o)return o();const{credential:T,select_by:k}=w;t({select_by:k,credential:T,clientId:l(w)})},...C}),h.style.height=this.containerHeightMap[g],(L=window.google)==null||L.accounts.id.renderButton(h,{type:a,size:g,text:s,theme:f,shape:u,width:y,locale:c,logo_alignment:G,click_listener:P}),i&&((I=window.google)==null||I.accounts.id.prompt(r)))}}function S(){var e;typeof window>"u"||(e=window.google)==null||e.accounts.id.disableAutoSelect()}function A(e,t,...o){return window.google?window.google.accounts.oauth2.hasGrantedAnyScope(e,t,...o):!1}function b(e,t,...o){return window.google?window.google.accounts.oauth2.hasGrantedAllScopes(e,t,...o):!1}return n.GoogleOAuthProvider=d,n.googleLogout=S,n.hasGrantedAllScopesGoogle=b,n.hasGrantedAnyScopeGoogle=A,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),n}({});