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 14:29:47 -0500
committerPaul Frazee <pfrazee@gmail.com>2022-06-14 14:29:47 -0500
commitcef133031e501f8f73c66a379de38b1041287743 (patch)
treeb628150cbee4facbcbfc06599baaf4c108949995 /src/api/auth.ts
parent09b78a46343088a2790dab067bd4fd8384957311 (diff)
downloadvoidsky-cef133031e501f8f73c66a379de38b1041287743.tar.zst
Add base auth & ucan request flow (web only)
Diffstat (limited to 'src/api/auth.ts')
-rw-r--r--src/api/auth.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/api/auth.ts b/src/api/auth.ts
new file mode 100644
index 000000000..2da8f2cc7
--- /dev/null
+++ b/src/api/auth.ts
@@ -0,0 +1,48 @@
+import * as auth from '@adxp/auth'
+import {isWeb} from '../platform/detection'
+import * as env from '../env'
+
+const SCOPE = auth.writeCap(
+  'did:key:z6MkfRiFMLzCxxnw6VMrHK8pPFt4QAHS3jX3XM87y9rta6kP',
+  'did:example:microblog',
+)
+
+export async function isAuthed(authStore: auth.BrowserStore) {
+  return await authStore.hasUcan(SCOPE)
+}
+
+export async function logout(authStore: auth.BrowserStore) {
+  await authStore.reset()
+}
+
+export async function parseUrlForUcan() {
+  // @ts-ignore window is defined -prf
+  const fragment = window.location.hash
+  if (fragment.length < 1) {
+    return undefined
+  }
+  try {
+    const ucan = await auth.parseLobbyResponseHashFragment(fragment)
+    // @ts-ignore window is defined -prf
+    window.location.hash = ''
+    return ucan
+  } catch (err) {
+    return undefined
+  }
+}
+
+export async function requestAppUcan(authStore: auth.BrowserStore) {
+  const did = await authStore.getDid()
+  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}`
+    return false
+  } else {
+    // TODO
+    console.log('TODO')
+  }
+  return false
+}