UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 1.2 kB
"use strict";var e=require("./internal-context-CeWKmK5u.js"),t=require("./events-context-7-Gs8c_9.js");require("ofetch"),require("react"),require("react/jsx-runtime"),require("@privy-io/js-sdk-core"),require("tinycolor2");class r{async authenticate(){if(!this.api)throw new e.PrivyClientError("Auth flow has no API instance");try{return await this.api.post(e.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 i=t.useAppConfig(),n=t.useEmitPrivyEvent(),{client:o,setUser:s,setAuthenticated:a,setIsNewUser:u}=e.usePrivyInternal();return{loginWithSsoToken:async()=>{if(!i.id||!o)throw new e.PrivyClientError("SDK not yet ready");try{let t=new r;o.startAuthFlow(t);let i=await o.authenticate(),c=i.user,h=i.isNewUser??!1;if(!c)throw new e.PrivyClientError("Unable to authenticate with SSO response");return s(c),u(h),a(!0),n("login","onComplete",{user:c,isNewUser:h,wasAlreadyAuthenticated:!1,loginMethod:"sso",loginAccount:null}),c}catch(t){throw n("login","onError",t.privyErrorCode||e.PrivyErrorCode.UNKNOWN_AUTH_ERROR),t}}}};