Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: sync user on startup

thecodrr2e80859bead4
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 498ed612e..b481f1c20 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -32,6 +32,8 @@ class Database {
this._validate();
this.user = new User(this);
+ await this.user.sync();
+
this.syncer = new Sync(this);
this.vault = new Vault(this);
this.conflicts = new Conflicts(this);
diff --git a/packages/core/models/user.js b/packages/core/models/user.js
index 416f755a8..fa71af9d7 100644
--- a/packages/core/models/user.js
+++ b/packages/core/models/user.js
@@ -10,6 +10,14 @@ export default class User {
this._context = db.context;
}
+ async sync() {
+ var user = await this.get();
+ if (!user) return;
+ user = await authRequest("users", undefined, true, true);
+ delete user.lastSynced;
+ await this.set(user);
+ }
+
get() {
return this._context.read("user");
}
@@ -29,14 +37,13 @@ export default class User {
async upgrade(refno) {
if (!refno) return;
await this.set({ refno, upgrading: true });
- const token = await this.token();
- let response = await authRequest(
- "upgrade",
- { refno },
- { Authorization: `Bearer ${token}` }
- );
- if (response.success) {
- await this.set({ refno: null, upgrading: false });
+ let response = await authRequest("upgrade", { refno }, true);
+ if (response.duration) {
+ await this.set({
+ refno: undefined,
+ trialExpiryDate: response.duration,
+ upgrading: undefined,
+ });
}
}
@@ -109,11 +116,19 @@ function userFromResponse(response, key) {
return user;
}
-async function authRequest(endpoint, data, headers = {}) {
+async function authRequest(endpoint, data, auth = false, get = false) {
+ var headers = {};
+ if (auth) {
+ const token = await this.token();
+ headers = {
+ Authorization: `Bearer ${token}`,
+ };
+ }
+
let response = await fetch(`${HOST}${endpoint}`, {
- method: "POST",
+ method: get ? "GET" : "POST",
headers: { ...HEADERS, ...headers },
- body: JSON.stringify(data),
+ body: get ? undefined : JSON.stringify(data),
});
if (response.ok) {