diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-06-15 22:33:16 -0500 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-06-15 22:33:16 -0500 |
commit | de87ec17d1673855fdfe2ccd125d734591969dd4 (patch) | |
tree | cc2380d8fd4bb6c97f034ed460638536c735a144 /src/platform | |
parent | 172ed1e2cd00ace0ace07f5782daa264f7a2764f (diff) | |
download | voidsky-de87ec17d1673855fdfe2ccd125d734591969dd4.tar.zst |
Fix web build
Diffstat (limited to 'src/platform')
-rw-r--r-- | src/platform/auth-flow.native.ts | 53 | ||||
-rw-r--r-- | src/platform/auth-flow.ts | 19 | ||||
-rw-r--r-- | src/platform/polyfills.native.ts | 3 | ||||
-rw-r--r-- | src/platform/polyfills.ts (renamed from src/platform/polyfills.web.ts) | 1 |
4 files changed, 74 insertions, 2 deletions
diff --git a/src/platform/auth-flow.native.ts b/src/platform/auth-flow.native.ts new file mode 100644 index 000000000..596632f17 --- /dev/null +++ b/src/platform/auth-flow.native.ts @@ -0,0 +1,53 @@ +import {Linking} from 'react-native' +import * as auth from '@adxp/auth' +import * as ucan from 'ucans' +import {InAppBrowser} from 'react-native-inappbrowser-reborn' +import {isWeb} from '../platform/detection' +import {extractHashFragment, makeAppUrl} from '../platform/urls' +import {ReactNativeStore, parseUrlForUcan} from '../state/auth' +import * as env from '../env' + +export async function requestAppUcan( + authStore: ReactNativeStore, + scope: ucan.Capability, +) { + const did = await authStore.getDid() + const returnUrl = makeAppUrl() + const fragment = auth.requestAppUcanHashFragment(did, scope, returnUrl) + const url = `${env.AUTH_LOBBY}#${fragment}` + + if (isWeb) { + // @ts-ignore window is defined -prf + window.location.href = url + return false + } + + if (await InAppBrowser.isAvailable()) { + // use in-app browser + const res = await InAppBrowser.openAuth(url, returnUrl, { + // iOS Properties + ephemeralWebSession: false, + // Android Properties + showTitle: false, + enableUrlBarHiding: true, + enableDefaultShare: false, + }) + if (res.type === 'success' && res.url) { + const fragment = extractHashFragment(res.url) + if (fragment) { + const ucan = await parseUrlForUcan(fragment) + if (ucan) { + await authStore.addUcan(ucan) + return true + } + } + } else { + console.log('Not completed', res) + return false + } + } else { + // use system browser + Linking.openURL(url) + } + return true +} diff --git a/src/platform/auth-flow.ts b/src/platform/auth-flow.ts new file mode 100644 index 000000000..b96fc58e9 --- /dev/null +++ b/src/platform/auth-flow.ts @@ -0,0 +1,19 @@ +import * as auth from '@adxp/auth' +import * as ucan from 'ucans' +import {makeAppUrl} from '../platform/urls' +import {ReactNativeStore} from '../state/auth' +import * as env from '../env' + +export async function requestAppUcan( + authStore: ReactNativeStore, + scope: ucan.Capability, +) { + const did = await authStore.getDid() + const returnUrl = makeAppUrl() + const fragment = auth.requestAppUcanHashFragment(did, scope, returnUrl) + const url = `${env.AUTH_LOBBY}#${fragment}` + + // @ts-ignore window is defined -prf + window.location.href = url + return false +} diff --git a/src/platform/polyfills.native.ts b/src/platform/polyfills.native.ts index 0c9f30582..dd1ecf7eb 100644 --- a/src/platform/polyfills.native.ts +++ b/src/platform/polyfills.native.ts @@ -1,12 +1,11 @@ -// import {generateSecureRandom} from 'react-native-securerandom' import {NativeModules} from 'react-native' const {AppSecureRandomModule} = NativeModules import {toByteArray} from 'base64-js' +// @ts-ignore we dont have types for this -prf import crypto from 'msrcrypto' import '@zxing/text-encoding' // TextEncoder / TextDecoder async function generateSecureRandom(bytes: number) { - console.log('a') return toByteArray( await AppSecureRandomModule.generateSecureRandomAsBase64(bytes), ) diff --git a/src/platform/polyfills.web.ts b/src/platform/polyfills.ts index c6035e5e3..ac31e55ef 100644 --- a/src/platform/polyfills.web.ts +++ b/src/platform/polyfills.ts @@ -1 +1,2 @@ // do nothing +export {} |