UNPKG

vue3-msal-plugin

Version:

Vue 3 plugin for integrating MSAL.js into your app, offering easy-to-use composables.

3 lines (2 loc) 2.89 kB
import{InteractionStatus as p,InteractionType as v}from"@azure/msal-browser";import{getCurrentInstance as b,toRefs as q,watch as A,ref as f}from"vue";import{PublicClientApplication as d}from"@azure/msal-browser";var S=n=>{try{return new d(n)}catch(e){throw console.error("Failed to create PublicClientApplication:",e),e}};import{reactive as I}from"vue";import{EventMessageUtils as C,EventType as c,InteractionStatus as P}from"@azure/msal-browser";function m(n,e){if(n.length!==e.length)return!1;let r=[...e];return n.every(o=>{let t=r.shift();return!o||!t?!1:o.homeAccountId===t.homeAccountId&&o.localAccountId===t.localAccountId&&o.username===t.username})}var y={install:(n,e)=>{let r=P.Startup,o=e.getAllAccounts(),t=I({instance:e,inProgress:r,accounts:o});n.config.globalProperties.$msal=t,e.addEventCallback(a=>{switch(a.eventType){case c.ACCOUNT_ADDED:case c.ACCOUNT_REMOVED:case c.LOGIN_SUCCESS:case c.SSO_SILENT_SUCCESS:case c.HANDLE_REDIRECT_END:case c.LOGIN_FAILURE:case c.SSO_SILENT_FAILURE:case c.LOGOUT_END:case c.ACQUIRE_TOKEN_SUCCESS:case c.ACQUIRE_TOKEN_FAILURE:{let l=e.getAllAccounts();m(l,t.accounts)||(t.accounts=l);break}}let s=C.getInteractionStatusFromEvent(a,t.inProgress);s!==null&&(t.inProgress=s)})}};var _={scopes:["User.Read"]};function g(){let n=b();if(!n)throw"useMsal() cannot be called outside the setup() function of a component";let{instance:e,accounts:r,inProgress:o}=q(n.appContext.config.globalProperties.$msal);if(!e.value||!r.value||!o.value)throw"Please install the msalPlugin";return o.value===p.Startup&&e.value.initialize().then(()=>{e.value.handleRedirectPromise().catch(t=>{console.error(t)})}),{instance:e.value,accounts:r,inProgress:o,loginRequest:_,callMsGraph:M}}function $(){let{accounts:n}=g(),e=f(n.value.length>0);return A(n,()=>{e.value=n.value.length>0}),e}function K(n,e){let{instance:r,inProgress:o}=g(),t=f(!1),a=f(null),s=f(null),l=async E=>{if(!t.value){t.value=!0;let h=E||e;if(o.value===p.Startup||o.value===p.HandleRedirect)try{let u=await r.handleRedirectPromise();if(u){a.value=u,s.value=null;return}}catch(u){a.value=null,s.value=u;return}try{let u=await r.acquireTokenSilent(h);a.value=u,s.value=null}catch{if(o.value!==p.None)return;n===v.Popup?r.loginPopup(h).then(i=>{a.value=i,s.value=null}).catch(i=>{s.value=i,a.value=null}):n===v.Redirect&&await r.loginRedirect(h).catch(i=>{s.value=i,a.value=null})}t.value=!1}},R=A(o,()=>{!a.value&&!s.value?l():R()});return l(),{acquireToken:l,result:a,error:s,inProgress:t}}var w={graphMeEndpoint:"https://graph.microsoft.com/v1.0/me"};async function M(n){let e=new Headers,r=`Bearer ${n}`;e.append("Authorization",r);let o={method:"GET",headers:e};return fetch(w.graphMeEndpoint,o).then(t=>t.json()).catch(t=>{throw console.log(t),t})}export{S as msalInstance,y as msalPlugin,$ as useIsAuthenticated,g as useMsal,K as useMsalAuthentication}; //# sourceMappingURL=index.js.map