UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 1.06 kB
import{P as t,aq as e,f as o,aa as r,h as n}from"./internal-context-eGSwROSk.mjs";import{k as s,n as i}from"./events-context-ClaboIDc.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(e,{})}catch(t){throw o(t)}}async link(){throw new t("Linking is not supported for the SSO token flow")}constructor(){this.meta={}}}function c(){let e=s(),o=i(),{client:c,setUser:h,setAuthenticated:l,setIsNewUser:w}=r();return{loginWithSsoToken:async()=>{if(!e.id||!c)throw new t("SDK not yet ready");try{let e=new a;c.startAuthFlow(e);let r=await c.authenticate(),n=r.user,s=r.isNewUser??!1;if(!n)throw new t("Unable to authenticate with SSO response");return h(n),w(s),l(!0),o("login","onComplete",{user:n,isNewUser:s,wasAlreadyAuthenticated:!1,loginMethod:"sso",loginAccount:null}),n}catch(t){throw o("login","onError",t.privyErrorCode||n.UNKNOWN_AUTH_ERROR),t}}}}export{c as useLoginWithSsoToken};