Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
server-ts: Convert routes/api/login
Elian Doranb1744c3867ef
diff --git a/src/routes/api/login.js b/src/routes/api/login.tssimilarity index 73%rename from src/routes/api/login.jsrename to src/routes/api/login.tsindex 9cb0ec8bc3..a4c41c4601 100644--- a/src/routes/api/login.js+++ b/src/routes/api/login.ts@@ -1,19 +1,20 @@"use strict";-const options = require('../../services/options');-const utils = require('../../services/utils');-const dateUtils = require('../../services/date_utils');-const instanceId = require('../../services/instance_id');-const passwordEncryptionService = require('../../services/encryption/password_encryption');-const protectedSessionService = require('../../services/protected_session');-const appInfo = require('../../services/app_info');-const eventService = require('../../services/events');-const sqlInit = require('../../services/sql_init');-const sql = require('../../services/sql');-const ws = require('../../services/ws');-const etapiTokenService = require('../../services/etapi_tokens');--function loginSync(req) {+import options = require('../../services/options');+import utils = require('../../services/utils');+import dateUtils = require('../../services/date_utils');+import instanceId = require('../../services/instance_id');+import passwordEncryptionService = require('../../services/encryption/password_encryption');+import protectedSessionService = require('../../services/protected_session');+import appInfo = require('../../services/app_info');+import eventService = require('../../services/events');+import sqlInit = require('../../services/sql_init');+import sql = require('../../services/sql');+import ws = require('../../services/ws');+import etapiTokenService = require('../../services/etapi_tokens');+import { Request } from 'express';++function loginSync(req: Request) {if (!sqlInit.schemaExists()) {return [500, { message: "DB schema does not exist, can't sync." }];}@@ -44,7 +45,7 @@ function loginSync(req) {return [400, { message: "Sync login credentials are incorrect. It looks like you're trying to sync two different initialized documents which is not possible." }];}- req.session.loggedIn = true;+ (req as any).session.loggedIn = true;return {instanceId: instanceId,@@ -52,7 +53,7 @@ function loginSync(req) {};}-function loginToProtectedSession(req) {+function loginToProtectedSession(req: Request) {const password = req.body.password;if (!passwordEncryptionService.verifyPassword(password)) {@@ -63,6 +64,12 @@ function loginToProtectedSession(req) {}const decryptedDataKey = passwordEncryptionService.getDataKey(password);+ if (!decryptedDataKey) {+ return {+ success: false,+ message: "Unable to obtain data key."+ }+ }protectedSessionService.setDataKey(decryptedDataKey);@@ -87,7 +94,7 @@ function touchProtectedSession() {protectedSessionService.touchProtectedSession();}-function token(req) {+function token(req: Request) {const password = req.body.password;if (!passwordEncryptionService.verifyPassword(password)) {@@ -102,7 +109,7 @@ function token(req) {return { token: authToken };}-module.exports = {+export = {loginSync,loginToProtectedSession,logoutFromProtectedSession,diff --git a/src/routes/routes.js b/src/routes/routes.jsindex 76c27672d2..7799cb415b 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -33,7 +33,7 @@ const recentChangesApiRoute = require('./api/recent_changes.js');const optionsApiRoute = require('./api/options.js');const passwordApiRoute = require('./api/password');const syncApiRoute = require('./api/sync');-const loginApiRoute = require('./api/login.js');+const loginApiRoute = require('./api/login');const recentNotesRoute = require('./api/recent_notes.js');const appInfoRoute = require('./api/app_info');const exportRoute = require('./api/export');diff --git a/src/services/events.ts b/src/services/events.tsindex fffcc3982a..327443a95b 100644--- a/src/services/events.ts+++ b/src/services/events.ts@@ -44,7 +44,7 @@ function subscribeBeccaLoader(eventTypes: EventType, listener: EventListener) {}}-function emit(eventType: string, data: any) {+function emit(eventType: string, data?: any) {const listeners = eventListeners[eventType];if (listeners) {