UNPKG

@privy-io/expo

Version:

Expo client for the Privy Auth API

2 lines (1 loc) 4.92 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 _chunkWWWQIJT5js = require('./chunk-WWWQIJT5.js');var _chunkQTVIWU3Njs = require('./chunk-QTVIWU3N.js');var _expolinking = require('expo-linking'); var y = _interopRequireWildcard(_expolinking); var j = _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 g = _interopRequireWildcard(_expoappleauthentication);function H(a){return _chunkQTVIWU3Njs.d.call(void 0, 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 (0, _jssdkcore.PrivyClientError)({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:""}function z(t){return _reactnative.Platform.select({android:t.replace("x.com","twitter.com"),default:t})}var _expowebbrowser = require('expo-web-browser'); var m = _interopRequireWildcard(_expowebbrowser);function Q(r){return _chunkQTVIWU3Njs.d.call(void 0, 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 (0, _jssdkcore.PrivyClientError)({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 (0, _jssdkcore.PrivyClientError)({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=_chunkQTVIWU3Njs.c.call(void 0, r,["action"]);let{oAuthState:s,setOAuthState:o,client:u}=_react.useContext.call(void 0, _chunkWWWQIJT5js.aa),l=_react.useRef.call(void 0, i);return _react.useEffect.call(void 0, ()=>{l.current=i},[i]),{start:_react.useCallback.call(void 0, function(Lt){return _chunkQTVIWU3Njs.d.call(void 0, this,arguments,function*({provider:c,redirectUri:d,isLegacyAppleIosBehaviorEnabled:$=!1,disableSignup:G,onAppleOAuthUserInfo:U}){var C,L,v,E;o({status:"loading"});let b=_chunkWWWQIJT5js.b.call(void 0, ),_=t?t==="login":!!!b;try{if(b&&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(!b&&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`"});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(_reactnative.Platform.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 (0, _jssdkcore.PrivyClientError)({error:"OAuth invalid credentials",code:_?"login_with_oauth_returned_with_invalid_credentials":"link_with_oauth_returned_with_invalid_credentials"});if(_){let e=_chunkWWWQIJT5js.a.call(void 0, ),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=_chunkWWWQIJT5js.g.call(void 0, 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(_chunkQTVIWU3Njs.b.call(void 0, _chunkQTVIWU3Njs.a.call(void 0, {},t),{action:"login"}));return{state:i,login:a}}exports.a = V; exports.b = St;