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