UNPKG

vue3-google-signin

Version:

Google Sign-in for Vue3 with Google Identity Service

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