@privy-io/expo
Version:
Expo client for the Privy Auth API
2 lines (1 loc) • 3.94 kB
JavaScript
import{a as N,aa as D,b as B,g as T}from"./chunk-WWWQIJT5.js";import{a as P,b as F,c as R,d as h}from"./chunk-QTVIWU3N.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 H(a){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 a;return(a=new URL(t).searchParams.get("state"))!=null?a:""}import{Platform as X}from"react-native";function z(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 M}from"@privy-io/js-sdk-core";function Q(r){return h(this,arguments,function*({oauthProviderUrl:t,isLogin:i,returnUrl:a}){var u,l;let s=yield m.openAuthSessionAsync(t,a,{createTask:!1});if(s.type===m.WebBrowserResultType.CANCEL||s.type===m.WebBrowserResultType.DISMISS)throw new M({error:"OAuth was cancelled",code:i?"login_with_oauth_was_cancelled_by_user":"link_with_oauth_was_cancelled_by_user"});if(s.type!=="success")throw new M({error:"OAuth session failed",code:i?"failed_to_complete_login_with_oauth":"failed_to_complete_link_with_oauth"});let o=j.parse(s.url).queryParams;return{authorizationCode:(u=o==null?void 0:o.privy_oauth_code)!=null?u:null,state:(l=o==null?void 0:o.privy_oauth_state)!=null?l:null,resultUrl:s.url}})}var V=(a={})=>{var r=a,{action:t}=r,i=R(r,["action"]);let{oAuthState:s,setOAuthState:o,client:u}=Z(D),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")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:n}=yield u.auth.oauth.generateURL(c,w);c==="twitter"&&(n=z(n));let p,k,A,f,I,W,O;if(et.OS==="ios"&&c==="apple"&&!$){let e=yield H({oauthProviderUrl:n,isLogin:_});A=e.authorizationCode,f=e.state,O=e,I="raw"}else{let e=yield Q({oauthProviderUrl:n,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 u.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 u.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 n=T(w);throw o({status:"error",error:n}),(E=(v=l.current).onError)==null||E.call(v,n),n}})},[u.auth.oauth,t,o]),state:s}};function St(t){let{state:i,start:a}=V(F(P({},t),{action:"login"}));return{state:i,login:a}}export{V as a,St as b};