about summary refs log tree commit diff
path: root/src/state/models/session.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-01-24 19:32:24 -0600
committerGitHub <noreply@github.com>2023-01-24 19:32:24 -0600
commit869f6c4e0e464b7f5be9ef5676210ae8844bd834 (patch)
treea9a823723099129bb25c4b57435925b481d54266 /src/state/models/session.ts
parent21f5f4de157a73b3c4406461b2a36555b1bff228 (diff)
downloadvoidsky-869f6c4e0e464b7f5be9ef5676210ae8844bd834.tar.zst
Initial pass at push notifications + some fixes to the session management (#91)
* Fix: test the session during resume to ensure it's valid

* Don't delete sessions for now

* Add notifee and request notif permissions on first login

* Set unread notifications badge on app icon

* Trigger a notifee card on new notifications

* Experimental: use react-native-background-fetch to check for notifications

* Add missing mocks

* Fix to resumeSession()
Diffstat (limited to 'src/state/models/session.ts')
-rw-r--r--src/state/models/session.ts38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/state/models/session.ts b/src/state/models/session.ts
index 89347af9a..77c1fb595 100644
--- a/src/state/models/session.ts
+++ b/src/state/models/session.ts
@@ -286,17 +286,33 @@ export class SessionModel {
    * Attempt to resume a session that we still have access tokens for.
    */
   async resumeSession(account: AccountData): Promise<boolean> {
-    if (account.accessJwt && account.refreshJwt) {
-      this.setState({
-        service: account.service,
-        accessJwt: account.accessJwt,
-        refreshJwt: account.refreshJwt,
-        handle: account.handle,
-        did: account.did,
-      })
-    } else {
+    if (!(account.accessJwt && account.refreshJwt && account.service)) {
       return false
     }
+
+    // test that the session is good
+    const api = AtpApi.service(account.service)
+    api.sessionManager.set({
+      refreshJwt: account.refreshJwt,
+      accessJwt: account.accessJwt,
+    })
+    try {
+      const sess = await api.com.atproto.session.get()
+      if (!sess.success || sess.data.did !== account.did) {
+        return false
+      }
+    } catch (_e) {
+      return false
+    }
+
+    // session is good, connect
+    this.setState({
+      service: account.service,
+      accessJwt: account.accessJwt,
+      refreshJwt: account.refreshJwt,
+      handle: account.handle,
+      did: account.did,
+    })
     return this.connect()
   }
 
@@ -345,14 +361,14 @@ export class SessionModel {
    * Close all sessions across all accounts.
    */
   async logout() {
-    if (this.hasSession) {
+    /*if (this.hasSession) {
       this.rootStore.api.com.atproto.session.delete().catch((e: any) => {
         this.rootStore.log.warn(
           '(Minor issue) Failed to delete session on the server',
           e,
         )
       })
-    }
+    }*/
     this.clearSessionTokensFromAccounts()
     this.rootStore.clearAll()
   }