about summary refs log tree commit diff
path: root/src/api/auth.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2022-06-14 20:29:24 -0500
committerPaul Frazee <pfrazee@gmail.com>2022-06-14 20:29:24 -0500
commitb2dd8d4f440243ac2eb12e7013d5a024b4e95f07 (patch)
tree42f78d330e2863a3229666c7d5fa09bec611e35b /src/api/auth.ts
parent5066f3ba815d586f7d0f30135166aacf937480d6 (diff)
downloadvoidsky-b2dd8d4f440243ac2eb12e7013d5a024b4e95f07.tar.zst
Open login flow in in-app browser for native (WIP)
Diffstat (limited to 'src/api/auth.ts')
-rw-r--r--src/api/auth.ts34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/api/auth.ts b/src/api/auth.ts
index 2da8f2cc7..60ff1a3f2 100644
--- a/src/api/auth.ts
+++ b/src/api/auth.ts
@@ -1,5 +1,8 @@
+import {Linking} from 'react-native'
 import * as auth from '@adxp/auth'
+import {InAppBrowser} from 'react-native-inappbrowser-reborn'
 import {isWeb} from '../platform/detection'
+import {makeAppUrl} from '../platform/urls'
 import * as env from '../env'
 
 const SCOPE = auth.writeCap(
@@ -33,16 +36,33 @@ export async function parseUrlForUcan() {
 
 export async function requestAppUcan(authStore: auth.BrowserStore) {
   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
-    const redirectTo = window.location.origin
-    const fragment = auth.requestAppUcanHashFragment(did, SCOPE, redirectTo)
-    // @ts-ignore window is defined -prf
-    window.location.href = `${env.AUTH_LOBBY}#${fragment}`
+    window.location.href = url
     return false
+  }
+
+  if (await InAppBrowser.isAvailable()) {
+    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) {
+      Linking.openURL(res.url)
+    } else {
+      console.error('Bad response', res)
+      return false
+    }
   } else {
-    // TODO
-    console.log('TODO')
+    Linking.openURL(url)
   }
-  return false
+  return true
 }