@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 1.13 kB
JavaScript
import{a as t,f as e,u as o,b as r}from"./internal-context-e-Eni5bG.mjs";import{a7 as n}from"./paths-3HW55qZg.mjs";import{u as s}from"./context-Bycd1SKV.mjs";import{a as i}from"./events-context-CI0iqAXA.mjs";import"ofetch";import"react";import"react/jsx-runtime";import"@privy-io/js-sdk-core";import"tinycolor2";class a{async authenticate(){if(!this.api)throw new t("Auth flow has no API instance");try{return await this.api.post(n,{})}catch(t){throw e(t)}}async link(){throw new t("Linking is not supported for the SSO token flow")}constructor(){this.meta={}}}function c(){let e=s(),n=i(),{client:c,setUser:h,setAuthenticated:l,setIsNewUser:w}=o();return{loginWithSsoToken:async()=>{if(!e.id||!c)throw new t("SDK not yet ready");try{let e=new a;c.startAuthFlow(e);let o=await c.authenticate(),r=o.user,s=o.isNewUser??!1;if(!r)throw new t("Unable to authenticate with SSO response");return h(r),w(s),l(!0),n("login","onComplete",{user:r,isNewUser:s,wasAlreadyAuthenticated:!1,loginMethod:"sso",loginAccount:null}),r}catch(t){throw n("login","onError",t.privyErrorCode||r.UNKNOWN_AUTH_ERROR),t}}}}export{c as useLoginWithSsoToken};