UNPKG

vue3-google-signin

Version:

Google Sign-in for Vue3 with Google Identity Service

2 lines (1 loc) 6.94 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("vue"),h=Symbol(),A=t.ref(!1),k=t.ref(!1),R=t.ref(!1),S=t.ref(0);let b=null;const J=()=>{const e=document.createElement("script");return e.src="https://accounts.google.com/gsi/client",e.async=!0,e.defer=!0,e},K=()=>{k.value=!0,b=J(),document.head.appendChild(b),b.onload=()=>{k.value=!1,A.value=!0},b.onerror=()=>{k.value=!1,R.value=!0}};t.watch(()=>S.value,(e,i)=>{e>0&&!A.value&&!k.value&&K()});function w(){return t.onMounted(()=>{S.value++}),t.onUnmounted(()=>{S.value--}),{scriptLoaded:t.readonly(A),scriptLoadError:t.readonly(R)}}const U=t.defineComponent({__name:"GoogleSignInButton",props:{oneTap:{type:Boolean},autoSelect:{type:Boolean},loginUri:{},cancelOnTapOutside:{type:Boolean},promptParentId:{},nonce:{},context:{},stateCookieDomain:{},uxMode:{},allowedParentOrigin:{},itpSupport:{type:Boolean},type:{},theme:{},size:{},text:{},shape:{},logoAlignment:{},width:{},locale:{}},emits:["success","error","intermediateIframeCloseCallback","nativeCallback","promptMomentNotification"],setup(e,{emit:i}){const o={large:40,medium:32,small:20},n=e,a=i,l=t.inject(h),r=t.ref(null),{scriptLoaded:s}=w();t.watchEffect(()=>{var c,d,_,p;s.value&&((c=window.google)==null||c.accounts.id.initialize({client_id:l,callback:f=>{if(!f.credential){a("error");return}a("success",f)},allowed_parent_origin:n.allowedParentOrigin,auto_select:n.autoSelect,cancel_on_tap_outside:n.cancelOnTapOutside,context:n.context,intermediate_iframe_close_callback:()=>{a("intermediateIframeCloseCallback")},itp_support:n.itpSupport,login_uri:n.loginUri,native_callback:f=>{a("nativeCallback",f)},nonce:n.nonce,prompt_parent_id:n.promptParentId,state_cookie_domain:n.stateCookieDomain,ux_mode:n.uxMode,use_fedcm_for_prompt:!0}),(_=window.google)==null||_.accounts.id.renderButton(r.value,{type:n.type,theme:n.theme,size:n.size,text:n.text,shape:n.shape,logo_alignment:n.logoAlignment,width:(d=n.width)==null?void 0:d.toString(),locale:n.locale}),n.oneTap&&((p=window.google)==null||p.accounts.id.prompt(f=>{a("promptMomentNotification",f)})))}),t.onUnmounted(()=>{var c;n.oneTap&&((c=window.google)==null||c.accounts.id.cancel())});const u=t.computed(()=>o[n.size||"large"]);return(c,d)=>(t.openBlock(),t.createElementBlock("div",{ref_key:"targetElement",ref:r,style:t.normalizeStyle({display:"inline-flex",height:u.value})},null,4))}});function F(e,i,...o){var n;return((n=window.google)==null?void 0:n.accounts.oauth2.hasGrantedAllScopes(e,i,...o))||!1}function Q(e,i,...o){var n;return((n=window.google)==null?void 0:n.accounts.oauth2.hasGrantedAnyScope(e,i,...o))||!1}function W(e,i){var o;(o=window.google)==null||o.accounts.oauth2.revoke(e,()=>{i==null||i()})}function G(e){const i="https://accounts.google.com/o/oauth2/v2/auth",o=new URLSearchParams({gsiwebsdk:"3",client_id:e.client_id,scope:e.scope,include_granted_scopes:"true",access_type:"offline",response_type:"code",prompt:"consent"});return e.hint&&o.append("hint",e.hint),e.hosted_domain&&o.append("hosted_domain",e.hosted_domain),e.redirect_uri===void 0?o.append("redirect_uri",window.origin):o.append("redirect_uri",e.redirect_uri),e.select_account===void 0?o.append("select_account","false"):o.append("select_account",`${e.select_account}`),e.enable_serial_consent===void 0?o.append("enable_serial_consent","false"):o.append("enable_serial_consent",`${e.enable_serial_consent}`),e.state&&o.append("state",e.state),`${i}?${o.toString()}`}function X(e={}){const{scope:i="",onError:o,onSuccess:n,...a}=e,{scriptLoaded:l}=w(),r=t.inject(h),s=t.ref(!1),u=t.ref(null);let c;return t.watchEffect(()=>{var f;if(s.value=!1,!l.value)return;const d=t.unref(i),p=`openid email profile ${Array.isArray(d)?d.join(" "):d}`;u.value=G({client_id:r,scope:p,...a}),c=(f=window.google)==null?void 0:f.accounts.oauth2.initCodeClient({client_id:r,scope:p,callback:m=>{if(m.error)return o==null?void 0:o(m);n==null||n(m)},...a}),s.value=!0}),{isReady:t.readonly(s),login:()=>c==null?void 0:c.requestCode(),codeRequestRedirectUrl:t.readonly(u)}}function Y(e){const{disableAutomaticPrompt:i=!1,onSuccess:o,onError:n,onPromptMomentNotification:a,onNativeCallback:l,onIntermediateIframeCloseCallback:r,autoSelect:s,loginUri:u,cancelOnTapOutside:c,promptParentId:d,nonce:_,context:p,stateCookieDomain:f,allowedParentOrigin:m,itpSupport:P}=e||{},{scriptLoaded:E}=w(),L=t.inject(h),v=t.ref(!1),j=()=>{var y;return v.value&&((y=window.google)==null?void 0:y.accounts.id.prompt(C=>a==null?void 0:a(C)))};return t.watchEffect(y=>{var I,T;if(v.value=!1,!E.value)return;const C=!t.unref(i),z=t.unref(s),B=t.unref(u),q=t.unref(d),$=t.unref(_),M=t.unref(p),V=t.unref(f),D=t.unref(m),N=t.unref(P),H=t.unref(c);(I=window.google)==null||I.accounts.id.initialize({client_id:L,callback:g=>{if(!g.credential){n==null||n();return}o==null||o(g)},native_callback:g=>{l==null||l(g)},intermediate_iframe_close_callback:()=>{r==null||r()},auto_select:z,allowed_parent_origin:D,context:M,itp_support:N,login_uri:B,nonce:$,prompt_parent_id:q,state_cookie_domain:V,cancel_on_tap_outside:H,use_fedcm_for_prompt:!0}),v.value=!0,C&&((T=window.google)==null||T.accounts.id.prompt(g=>a==null?void 0:a(g))),y(()=>{var g;(g=window.google)==null||g.accounts.id.cancel()})}),{isReady:t.readonly(v),login:j}}function Z(e={}){const{scope:i="",onError:o,onSuccess:n,...a}=e,{scriptLoaded:l}=w(),r=t.inject(h),s=t.ref(!1);let u;return t.watchEffect(()=>{var _;if(s.value=!1,!l.value)return;const c=t.unref(i),d=Array.isArray(c)?c.join(" "):c;u=(_=window.google)==null?void 0:_.accounts.oauth2.initTokenClient({client_id:r,scope:`openid email profile ${d}`,callback:p=>{if(p.error)return o==null?void 0:o(p);n==null||n(p)},...a}),s.value=!0}),{isReady:t.readonly(s),login:c=>u==null?void 0:u.requestAccessToken(c)}}function ee(e,i){var o;(o=window.google)==null||o.accounts.id.revoke(e,n=>{i==null||i(n)})}function te(e){const o=e.split(".")[1].replace(/-/g,"+").replace(/_/g,"/"),n=decodeURIComponent(window.atob(o).split("").map(l=>`%${("00"+l.charCodeAt(0).toString(16)).slice(-2)}`).join("")),a=JSON.parse(n);return{email:a.email,email_verified:a.email_verified,hd:a.hd,family_name:a.family_name,given_name:a.given_name,name:a.name,picture:a.picture,id:a.sub,iat:a.iat,exp:a.exp}}const O="GoogleSignInPlugin",x=e=>`[${O}]: ${e}`,ne={install(e,i){if(!i)throw new Error(x("initialize plugin by passing an options object"));if(!i.clientId||i.clientId&&i.clientId.trim().length===0)throw new Error(x("clientId is required to initialize"));e.provide(h,i.clientId),e.component("GoogleSignInButton",U)}};exports.GoogleSignInButton=U;exports.PLUGIN_NAME=O;exports.buildCodeRequestRedirectUrl=G;exports.decodeCredential=te;exports.default=ne;exports.hasGrantedAllScopes=F;exports.hasGrantedAnyScopes=Q;exports.idRevoke=ee;exports.revokeAccessToken=W;exports.useCodeClient=X;exports.useGsiScript=w;exports.useOneTap=Y;exports.useTokenClient=Z;