FIX: Force encrypt reactivation.

FIX: Force encrypt reactivation.

diff --git a/assets/javascripts/discourse/initializers/hook-logout.js.es6 b/assets/javascripts/discourse/initializers/hook-logout.js.es6
index 0dc7441..a295ce2 100644
--- a/assets/javascripts/discourse/initializers/hook-logout.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-logout.js.es6
@@ -1,11 +1,15 @@
 import { deleteDb } from "discourse/plugins/discourse-encrypt/lib/database";
+import {
+  ENCRYPT_ACTIVE,
+  getEncryptionStatus
+} from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 export default {
   name: "hook-logout",
 
   initialize(container) {
     const currentUser = container.lookup("current-user:main");
-    if (!currentUser) {
+    if (!currentUser || getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
       deleteDb();
     }
   }
diff --git a/assets/javascripts/lib/database.js.es6 b/assets/javascripts/lib/database.js.es6
index 37bb2e4..6be36da 100644
--- a/assets/javascripts/lib/database.js.es6
+++ b/assets/javascripts/lib/database.js.es6
@@ -7,6 +7,7 @@ import {
  * @var {String} DB_NAME Name of IndexedDb used for storing keypairs.
  */
 export const DB_NAME = "discourse-encrypt";
+export const DB_VERSION = "discourse-encrypt-version";
 
 /**
  * Checks if this is running in Safari or DiscourseHub app for iOS.
@@ -81,9 +82,10 @@ export function saveDbIdentity(identity) {
   */
 
   if (isSafari) {
-    return exportIdentity(identity).then(exported =>
-      window.localStorage.setItem(DB_NAME, exported.private)
-    );
+    return exportIdentity(identity).then(exported => {
+      window.localStorage.setItem(DB_NAME, exported.private);
+      window.localStorage.setItem(DB_VERSION, exported.private);
+    });
   }
 
   return new Ember.RSVP.Promise((resolve, reject) => {
@@ -99,6 +101,7 @@ export function saveDbIdentity(identity) {
       let dataReq = st.add(identity);
       dataReq.onsuccess = dataEvt => {
         window.localStorage.setItem(DB_NAME, true);
+        window.localStorage.setItem(DB_VERSION, identity.version);
         resolve(dataEvt);
         db.close();
       };
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index 0d410ee..8cb30f0 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -1,6 +1,7 @@
 import { ajax } from "discourse/lib/ajax";
 import {
   DB_NAME,
+  DB_VERSION,
   loadDbIdentity,
   saveDbIdentity
 } from "discourse/plugins/discourse-encrypt/lib/database";
@@ -209,7 +210,10 @@ export function getEncryptionStatus(user) {
     return ENCRYPT_DISABLED;
   }
 
-  if (!window.localStorage.getItem(DB_NAME)) {
+  if (
+    !window.localStorage.getItem(DB_NAME) ||
+    !window.localStorage.getItem(DB_VERSION)
+  ) {
     return ENCRYPT_ENABLED;
   }

GitHub sha: b1c0f3dd

1 Like