1import Constants, { ExecutionEnvironment } from 'expo-constants'; 2import * as Linking from 'expo-linking'; 3import { Platform } from 'expo-modules-core'; 4import qs from 'qs'; 5export class SessionUrlProvider { 6 static BASE_URL = `https://auth.expo.io`; 7 static SESSION_PATH = 'expo-auth-session'; 8 getDefaultReturnUrl(urlPath, options) { 9 const queryParams = SessionUrlProvider.getHostAddressQueryParams(); 10 let path = SessionUrlProvider.SESSION_PATH; 11 if (urlPath) { 12 path = [path, SessionUrlProvider.removeLeadingSlash(urlPath)].filter(Boolean).join('/'); 13 } 14 return Linking.createURL(path, { 15 // The redirect URL doesn't matter for the proxy as long as it's valid, so silence warnings if needed. 16 scheme: options?.scheme ?? Linking.resolveScheme({ isSilent: true }), 17 queryParams, 18 isTripleSlashed: options?.isTripleSlashed, 19 }); 20 } 21 getStartUrl(authUrl, returnUrl, projectNameForProxy) { 22 if (Platform.OS === 'web' && !Platform.isDOMAvailable) { 23 // Return nothing in SSR envs 24 return ''; 25 } 26 const queryString = qs.stringify({ 27 authUrl, 28 returnUrl, 29 }); 30 return `${this.getRedirectUrl({ projectNameForProxy })}/start?${queryString}`; 31 } 32 getRedirectUrl(options) { 33 if (Platform.OS === 'web') { 34 if (Platform.isDOMAvailable) { 35 return [window.location.origin, options.urlPath].filter(Boolean).join('/'); 36 } 37 else { 38 // Return nothing in SSR envs 39 return ''; 40 } 41 } 42 const legacyExpoProjectFullName = options.projectNameForProxy || Constants.expoConfig?.originalFullName; 43 if (!legacyExpoProjectFullName) { 44 let nextSteps = ''; 45 if (__DEV__) { 46 if (Constants.executionEnvironment === ExecutionEnvironment.Bare) { 47 nextSteps = 48 ' Please ensure you have the latest version of expo-constants installed and rebuild your native app. You can verify that originalFullName is defined by running `expo config --type public` and inspecting the output.'; 49 } 50 else if (Constants.executionEnvironment === ExecutionEnvironment.StoreClient) { 51 nextSteps = 52 ' Please report this as a bug with the contents of `expo config --type public`.'; 53 } 54 } 55 if (Constants.manifest2) { 56 nextSteps = 57 ' Prefer AuthRequest in combination with an Expo Development Client build of your application.' + 58 ' To continue using the AuthSession proxy, specify the project full name (@owner/slug) using the projectNameForProxy option.'; 59 } 60 throw new Error('Cannot use the AuthSession proxy because the project full name is not defined.' + nextSteps); 61 } 62 const redirectUrl = `${SessionUrlProvider.BASE_URL}/${legacyExpoProjectFullName}`; 63 if (__DEV__) { 64 SessionUrlProvider.warnIfAnonymous(legacyExpoProjectFullName, redirectUrl); 65 // TODO: Verify with the dev server that the manifest is up to date. 66 } 67 return redirectUrl; 68 } 69 static getHostAddressQueryParams() { 70 let hostUri = Constants.expoConfig?.hostUri; 71 if (!hostUri && 72 (ExecutionEnvironment.StoreClient === Constants.executionEnvironment || 73 Linking.resolveScheme({}))) { 74 if (!Constants.linkingUri) { 75 hostUri = ''; 76 } 77 else { 78 // we're probably not using up-to-date xdl, so just fake it for now 79 // we have to remove the /--/ on the end since this will be inserted again later 80 hostUri = SessionUrlProvider.removeScheme(Constants.linkingUri).replace(/\/--(\/.*)?$/, ''); 81 } 82 } 83 if (!hostUri) { 84 return undefined; 85 } 86 const uriParts = hostUri?.split('?'); 87 try { 88 return qs.parse(uriParts?.[1]); 89 } 90 catch { } 91 return undefined; 92 } 93 static warnIfAnonymous(id, url) { 94 if (id.startsWith('@anonymous/')) { 95 console.warn(`You are not currently signed in to Expo on your development machine. As a result, the redirect URL for AuthSession will be "${url}". If you are using an OAuth provider that requires adding redirect URLs to an allow list, we recommend that you do not add this URL -- instead, you should sign in to Expo to acquire a unique redirect URL. Additionally, if you do decide to publish this app using Expo, you will need to register an account to do it.`); 96 } 97 } 98 static removeScheme(url) { 99 return url.replace(/^[a-zA-Z0-9+.-]+:\/\//, ''); 100 } 101 static removeLeadingSlash(url) { 102 return url.replace(/^\//, ''); 103 } 104} 105export default new SessionUrlProvider(); 106//# sourceMappingURL=SessionUrlProvider.js.map