mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
server-ts: Convert routes/login
This commit is contained in:
parent
c2eefad287
commit
7fe6d1ab4d
@ -1,15 +1,17 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const utils = require('../services/utils');
|
import utils = require('../services/utils');
|
||||||
const optionService = require('../services/options');
|
import optionService = require('../services/options');
|
||||||
const myScryptService = require('../services/encryption/my_scrypt');
|
import myScryptService = require('../services/encryption/my_scrypt');
|
||||||
const log = require('../services/log');
|
import log = require('../services/log');
|
||||||
const passwordService = require('../services/encryption/password');
|
import passwordService = require('../services/encryption/password');
|
||||||
const assetPath = require('../services/asset_path');
|
import assetPath = require('../services/asset_path');
|
||||||
const appPath = require('../services/app_path');
|
import appPath = require('../services/app_path');
|
||||||
const ValidationError = require('../errors/validation_error');
|
import ValidationError = require('../errors/validation_error');
|
||||||
|
import { Request, Response } from 'express';
|
||||||
|
import { AppRequest } from './route-interface';
|
||||||
|
|
||||||
function loginPage(req, res) {
|
function loginPage(req: Request, res: Response) {
|
||||||
res.render('login', {
|
res.render('login', {
|
||||||
failedAuth: false,
|
failedAuth: false,
|
||||||
assetPath: assetPath,
|
assetPath: assetPath,
|
||||||
@ -17,7 +19,7 @@ function loginPage(req, res) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function setPasswordPage(req, res) {
|
function setPasswordPage(req: Request, res: Response) {
|
||||||
res.render('set_password', {
|
res.render('set_password', {
|
||||||
error: false,
|
error: false,
|
||||||
assetPath: assetPath,
|
assetPath: assetPath,
|
||||||
@ -25,7 +27,7 @@ function setPasswordPage(req, res) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function setPassword(req, res) {
|
function setPassword(req: Request, res: Response) {
|
||||||
if (passwordService.isPasswordSet()) {
|
if (passwordService.isPasswordSet()) {
|
||||||
throw new ValidationError("Password has been already set");
|
throw new ValidationError("Password has been already set");
|
||||||
}
|
}
|
||||||
@ -55,7 +57,7 @@ function setPassword(req, res) {
|
|||||||
res.redirect('login');
|
res.redirect('login');
|
||||||
}
|
}
|
||||||
|
|
||||||
function login(req, res) {
|
function login(req: AppRequest, res: Response) {
|
||||||
const guessedPassword = req.body.password;
|
const guessedPassword = req.body.password;
|
||||||
|
|
||||||
if (verifyPassword(guessedPassword)) {
|
if (verifyPassword(guessedPassword)) {
|
||||||
@ -83,7 +85,7 @@ function login(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function verifyPassword(guessedPassword) {
|
function verifyPassword(guessedPassword: string) {
|
||||||
const hashed_password = utils.fromBase64(optionService.getOption('passwordVerificationHash'));
|
const hashed_password = utils.fromBase64(optionService.getOption('passwordVerificationHash'));
|
||||||
|
|
||||||
const guess_hashed = myScryptService.getVerificationHash(guessedPassword);
|
const guess_hashed = myScryptService.getVerificationHash(guessedPassword);
|
||||||
@ -91,7 +93,7 @@ function verifyPassword(guessedPassword) {
|
|||||||
return guess_hashed.equals(hashed_password);
|
return guess_hashed.equals(hashed_password);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logout(req, res) {
|
function logout(req: AppRequest, res: Response) {
|
||||||
req.session.regenerate(() => {
|
req.session.regenerate(() => {
|
||||||
req.session.loggedIn = false;
|
req.session.loggedIn = false;
|
||||||
|
|
||||||
@ -100,7 +102,7 @@ function logout(req, res) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export = {
|
||||||
loginPage,
|
loginPage,
|
||||||
setPasswordPage,
|
setPasswordPage,
|
||||||
setPassword,
|
setPassword,
|
16
src/routes/route-interface.ts
Normal file
16
src/routes/route-interface.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { Request } from "express";
|
||||||
|
|
||||||
|
export interface AppRequest extends Request {
|
||||||
|
headers: {
|
||||||
|
authorization?: string;
|
||||||
|
"trilium-cred"?: string;
|
||||||
|
}
|
||||||
|
session: {
|
||||||
|
loggedIn: boolean;
|
||||||
|
cookie: {
|
||||||
|
maxAge: number;
|
||||||
|
expires: boolean
|
||||||
|
};
|
||||||
|
regenerate: (callback: () => void) => void;
|
||||||
|
}
|
||||||
|
}
|
@ -18,7 +18,7 @@ const ValidationError = require('../errors/validation_error');
|
|||||||
|
|
||||||
// page routes
|
// page routes
|
||||||
const setupRoute = require('./setup');
|
const setupRoute = require('./setup');
|
||||||
const loginRoute = require('./login.js');
|
const loginRoute = require('./login');
|
||||||
const indexRoute = require('./index.js');
|
const indexRoute = require('./index.js');
|
||||||
|
|
||||||
// API routes
|
// API routes
|
||||||
|
@ -8,19 +8,10 @@ import passwordEncryptionService = require('./encryption/password_encryption');
|
|||||||
import config = require('./config');
|
import config = require('./config');
|
||||||
import passwordService = require('./encryption/password');
|
import passwordService = require('./encryption/password');
|
||||||
import type { NextFunction, Request, Response } from 'express';
|
import type { NextFunction, Request, Response } from 'express';
|
||||||
|
import { AppRequest } from '../routes/route-interface';
|
||||||
|
|
||||||
const noAuthentication = config.General && config.General.noAuthentication === true;
|
const noAuthentication = config.General && config.General.noAuthentication === true;
|
||||||
|
|
||||||
interface AppRequest extends Request {
|
|
||||||
headers: {
|
|
||||||
authorization?: string;
|
|
||||||
"trilium-cred"?: string;
|
|
||||||
}
|
|
||||||
session: {
|
|
||||||
loggedIn: boolean;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkAuth(req: AppRequest, res: Response, next: NextFunction) {
|
function checkAuth(req: AppRequest, res: Response, next: NextFunction) {
|
||||||
if (!sqlInit.isDbInitialized()) {
|
if (!sqlInit.isDbInitialized()) {
|
||||||
res.redirect("setup");
|
res.redirect("setup");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user