A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: use access_token from response instead of via errors
Abdullah Atta8c87d0010212
diff --git a/packages/core/api/mfa-manager.js b/packages/core/api/mfa-manager.jsindex 73f525710..252b95573 100644--- a/packages/core/api/mfa-manager.js+++ b/packages/core/api/mfa-manager.js@@ -123,11 +123,11 @@ class MFAManager {/*** @param {"sms" | "email"} method- * @param {string} phoneNumber* @returns*/- async sendCode(method, token) {- if (!token) throw new Error("Token is required to make this request.");+ async sendCode(method) {+ const token = await this.tokenManager.getAccessToken();+ if (!token) throw new Error("Unauthorized.");return await http.post(`${constants.AUTH_HOST}${ENDPOINTS.send}`,diff --git a/packages/core/api/token-manager.js b/packages/core/api/token-manager.jsindex 4d61424bf..f965cc2c6 100644--- a/packages/core/api/token-manager.js+++ b/packages/core/api/token-manager.js@@ -49,7 +49,8 @@ class TokenManager {accessToken: token.access_token.slice(0, 10)});- if (forceRenew || (renew && this._isTokenExpired(token))) {+ const isExpired = renew && this._isTokenExpired(token);+ if (this._isTokenRefreshable(token) && (forceRenew || isExpired)) {await this._refreshToken(forceRenew);return await this.getToken();}@@ -63,6 +64,12 @@ class TokenManager {return Date.now() >= expiryMs;}+ _isTokenRefreshable(token) {+ const { scope, refresh_token } = token;+ const scopes = scope.split(" ");+ return scopes.includes("offline_access") && Boolean(refresh_token);+ }+async getAccessToken(forceRenew = false) {return await getSafeToken(async () => {const token = await this.getToken(true, forceRenew);diff --git a/packages/core/api/user-manager.js b/packages/core/api/user-manager.jsindex 9b6d81f32..9ab795926 100644--- a/packages/core/api/user-manager.js+++ b/packages/core/api/user-manager.js@@ -83,33 +83,42 @@ class UserManager {email = email.toLowerCase();- await http.post(`${constants.AUTH_HOST}${ENDPOINTS.token}`, {+ const result = await http.post(`${constants.AUTH_HOST}${ENDPOINTS.token}`, {email,grant_type: "email",client_id: "notesnook"});++ await this.tokenManager.saveToken(result);+ return result.additional_data;}- async authenticateMultiFactorCode(code, method, token) {- if (!code || !method || !token)- throw new Error("code, method & token are required.");+ async authenticateMultiFactorCode(code, method) {+ if (!code || !method) throw new Error("code & method are required.");- await http.post(- `${constants.AUTH_HOST}${ENDPOINTS.token}`,- {- grant_type: "mfa",- client_id: "notesnook",- "mfa:code": code,- "mfa:method": method- },- token+ const token = await this.tokenManager.getAccessToken();+ if (!token) throw new Error("Unauthorized.");++ await this.tokenManager.saveToken(+ await http.post(+ `${constants.AUTH_HOST}${ENDPOINTS.token}`,+ {+ grant_type: "mfa",+ client_id: "notesnook",+ "mfa:code": code,+ "mfa:method": method+ },+ token+ ));return true;}- async authenticatePassword(email, password, token, hashedPassword = null) {- if (!email || !password || !token)- throw new Error("email, password & token are required.");+ async authenticatePassword(email, password, hashedPassword = null) {+ if (!email || !password) throw new Error("email & password are required.");++ const token = await this.tokenManager.getAccessToken();+ if (!token) throw new Error("Unauthorized.");email = email.toLowerCase();if (!hashedPassword) {