UNPKG

@privy-io/expo

Version:

Expo client for the Privy Auth API

2 lines (1 loc) 4.72 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }var _chunkC462C7XXjs = require('./chunk-C462C7XX.js');var _chunkOWCFWPALjs = require('./chunk-OWCFWPAL.js');var _expolinking = require('expo-linking'); var O = _interopRequireWildcard(_expolinking); var D = _interopRequireWildcard(_expolinking);var _react = require('react');var _reactnative = require('react-native');var _jssdkcore = require('@privy-io/js-sdk-core');var _expoappleauthentication = require('expo-apple-authentication'); var f = _interopRequireWildcard(_expoappleauthentication);function H(a){return _chunkOWCFWPALjs.d.call(void 0, 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 (0, _jssdkcore.PrivyClientError)({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:""}function z(t){return _reactnative.Platform.select({android:t.replace("x.com","twitter.com"),default:t})}var _expowebbrowser = require('expo-web-browser'); var g = _interopRequireWildcard(_expowebbrowser);function j(o){return _chunkOWCFWPALjs.d.call(void 0, 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 (0, _jssdkcore.PrivyClientError)({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 (0, _jssdkcore.PrivyClientError)({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=_chunkOWCFWPALjs.c.call(void 0, o,["action"]);let{oAuthState:s,setOAuthState:i,client:u}=_react.useContext.call(void 0, _chunkC462C7XXjs._),l=_react.useRef.call(void 0, e);return _react.useEffect.call(void 0, ()=>{l.current=e},[e]),{start:_react.useCallback.call(void 0, function(Ct){return _chunkOWCFWPALjs.d.call(void 0, this,arguments,function*({provider:c,redirectUri:G,isLegacyAppleIosBehaviorEnabled:J=!1,disableSignup:K,onAppleOAuthUserInfo:W}){var I,S,v,C;i({status:"loading"});let y=_chunkC462C7XXjs.b.call(void 0, ),m=t?t==="login":!!!y;try{if(y&&t==="login")throw new (0, _jssdkcore.PrivyClientError)({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 (0, _jssdkcore.PrivyClientError)({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(_reactnative.Platform.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 (0, _jssdkcore.PrivyClientError)({error:"OAuth invalid credentials",code:m?"login_with_oauth_returned_with_invalid_credentials":"link_with_oauth_returned_with_invalid_credentials"});if(m){let r=_chunkC462C7XXjs.a.call(void 0, ),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=_chunkC462C7XXjs.g.call(void 0, 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(_chunkOWCFWPALjs.b.call(void 0, _chunkOWCFWPALjs.a.call(void 0, {},t),{action:"login"}));return{state:e,login:a}}exports.a = Q; exports.b = It;