UNPKG

@privy-io/expo

Version:

Expo client for the Privy Auth API

2 lines (1 loc) 3.96 kB
import{a as N,b as B,fa as M,g as T}from"./chunk-ZDMK7NFI.js";import{b as P,c as F,d as R,e as h}from"./chunk-K5SPDXVF.js";import*as y from"expo-linking";import{useCallback as Y,useContext as Z,useEffect as q,useRef as tt}from"react";import{Platform as et}from"react-native";import{PrivyClientError as S}from"@privy-io/js-sdk-core";import*as g from"expo-apple-authentication";import{PrivyClientError as J}from"@privy-io/js-sdk-core";function D(s){return h(this,arguments,function*({oauthProviderUrl:t,isLogin:i}){try{return yield g.signInAsync({state:K(t),requestedScopes:[g.AppleAuthenticationScope.EMAIL,g.AppleAuthenticationScope.FULL_NAME]})}catch(r){throw r instanceof Error&&"code"in r&&r.code==="ERR_REQUEST_CANCELED"?new J({error:"Apple login was cancelled",code:i?"login_with_oauth_was_cancelled_by_user":"link_with_oauth_was_cancelled_by_user"}):r}})}function K(t){var s;return(s=new URL(t).searchParams.get("state"))!=null?s:""}import{Platform as X}from"react-native";function H(t){return X.select({android:t.replace("x.com","twitter.com"),default:t})}import*as j from"expo-linking";import*as m from"expo-web-browser";import{PrivyClientError as z}from"@privy-io/js-sdk-core";function Q(r){return h(this,arguments,function*({oauthProviderUrl:t,isLogin:i,returnUrl:s}){var n,l;let u=yield m.openAuthSessionAsync(t,s,{createTask:!1});if(u.type===m.WebBrowserResultType.CANCEL||u.type===m.WebBrowserResultType.DISMISS)throw new z({error:"OAuth was cancelled",code:i?"login_with_oauth_was_cancelled_by_user":"link_with_oauth_was_cancelled_by_user"});if(u.type!=="success")throw new z({error:"OAuth session failed",code:i?"failed_to_complete_login_with_oauth":"failed_to_complete_link_with_oauth"});let o=j.parse(u.url).queryParams;return{authorizationCode:(n=o==null?void 0:o.privy_oauth_code)!=null?n:null,state:(l=o==null?void 0:o.privy_oauth_state)!=null?l:null,resultUrl:u.url}})}var V=(s={})=>{var r=s,{action:t}=r,i=R(r,["action"]);let{oAuthState:u,setOAuthState:o,client:n}=Z(M),l=tt(i);return q(()=>{l.current=i},[i]),{start:Y(function(Lt){return h(this,arguments,function*({provider:c,redirectUri:d,isLegacyAppleIosBehaviorEnabled:$=!1,disableSignup:G,onAppleOAuthUserInfo:U}){var C,L,v,E;o({status:"loading"});let b=B(),_=t?t==="login":!!!b;try{if(b&&t==="login"&&!n.isMultiUserModeEnabled)throw new S({code:"attempted_login_with_oauth_while_already_logged_in",error:"Already logged in, if trying to link an OAuth account use `useLinkWithOAuth`"});if(!b&&t==="link")throw new S({code:"attempted_link_oauth_before_logged_in",error:"Must be logged in to link an OAuth account, use `useLoginWithOAuth`"});if(__DEV__&&d!=null&&d.includes("://")){let e=d.split("://")[0];console.warn(`redirectUri includes a scheme prefix (${e}://), it should be a path instead, such as '/authenticated'`)}let w=y.createURL(d||"/"),{url:a}=yield n.auth.oauth.generateURL(c,w);c==="twitter"&&(a=H(a));let p,k,A,f,I,W,O;if(et.OS==="ios"&&c==="apple"&&!$){let e=yield D({oauthProviderUrl:a,isLogin:_});A=e.authorizationCode,f=e.state,O=e,I="raw"}else{let e=yield Q({oauthProviderUrl:a,isLogin:_,returnUrl:w});A=e.authorizationCode,f=e.state,W=e.resultUrl}if(!A||!f)throw new S({error:"OAuth invalid credentials",code:_?"login_with_oauth_returned_with_invalid_credentials":"link_with_oauth_returned_with_invalid_credentials"});if(_){let e=N(),x=yield n.auth.oauth.loginWithCode(A,f,c,I,G?"no-signup":"login-or-sign-up",{embedded:e==null?void 0:e.embedded});p=x.user,k=x.is_new_user}else({user:p}=yield n.auth.oauth.linkWithCode(A,f,c,I));return O&&U&&U({fullName:O.fullName,email:O.email}),(L=(C=l.current).onSuccess)==null||L.call(C,p,k),W&&(yield y.openURL(W.split("?")[0])),o({status:"done",isNewUser:k}),p!=null?p:void 0}catch(w){let a=T(w);throw o({status:"error",error:a}),(E=(v=l.current).onError)==null||E.call(v,a),a}})},[n.auth.oauth,t,o]),state:u}};function St(t){let{state:i,start:s}=V(F(P({},t),{action:"login"}));return{state:i,login:s}}export{V as a,St as b};