UNPKG

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