UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 1.27 kB
"use strict";var e=require("./internal-context-NWsAL807.js"),t=require("./paths-DizMb-lU.js"),r=require("./context-DFq1obBO.js"),i=require("./events-context-Di6--rDg.js");require("ofetch"),require("react"),require("react/jsx-runtime"),require("@privy-io/js-sdk-core"),require("tinycolor2");class n{async authenticate(){if(!this.api)throw new e.PrivyClientError("Auth flow has no API instance");try{return await this.api.post(t.ssoAuthenticatePath,{})}catch(t){throw e.formatApiError(t)}}async link(){throw new e.PrivyClientError("Linking is not supported for the SSO token flow")}constructor(){this.meta={}}}exports.useLoginWithSsoToken=function(){let t=r.useAppConfig(),o=i.useEmitPrivyEvent(),{client:s,setUser:a,setAuthenticated:u,setIsNewUser:c}=e.usePrivyInternal();return{loginWithSsoToken:async()=>{if(!t.id||!s)throw new e.PrivyClientError("SDK not yet ready");try{let t=new n;s.startAuthFlow(t);let r=await s.authenticate(),i=r.user,h=r.isNewUser??!1;if(!i)throw new e.PrivyClientError("Unable to authenticate with SSO response");return a(i),c(h),u(!0),o("login","onComplete",{user:i,isNewUser:h,wasAlreadyAuthenticated:!1,loginMethod:"sso",loginAccount:null}),i}catch(t){throw o("login","onError",t.privyErrorCode||e.PrivyErrorCode.UNKNOWN_AUTH_ERROR),t}}}};