From 359a490ae339cdd11a8dc8731c0a93774c955fda Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 18 Apr 2022 15:21:58 +0800 Subject: [PATCH] Fix #1510 --- extra/reset-password.js | 3 ++- server/model/user.js | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/extra/reset-password.js b/extra/reset-password.js index 160ef0a3..8036a456 100644 --- a/extra/reset-password.js +++ b/extra/reset-password.js @@ -4,6 +4,7 @@ const Database = require("../server/database"); const { R } = require("redbean-node"); const readline = require("readline"); const { initJWTSecret } = require("../server/util-server"); +const User = require("../server/model/user"); const args = require("args-parser")(process.argv); const rl = readline.createInterface({ input: process.stdin, @@ -30,7 +31,7 @@ const main = async () => { let confirmPassword = await question("Confirm New Password: "); if (password === confirmPassword) { - await user.resetPassword(password); + await User.resetPassword(user.id, password); // Reset all sessions by reset jwt secret await initJWTSecret(); diff --git a/server/model/user.js b/server/model/user.js index d1d3d200..b243f87f 100644 --- a/server/model/user.js +++ b/server/model/user.js @@ -5,17 +5,29 @@ const { R } = require("redbean-node"); class User extends BeanModel { /** - * Direct execute, no need R.store() + * + * Fix #1510, as in the context reset-password.js, there is no auto model mapping. Call this static function instead. + * @param userID * @param newPassword * @returns {Promise} */ - async resetPassword(newPassword) { + static async resetPassword(userID, newPassword) { await R.exec("UPDATE `user` SET password = ? WHERE id = ? ", [ passwordHash.generate(newPassword), - this.id + userID ]); + } + + /** + * + * @param newPassword + * @returns {Promise} + */ + async resetPassword(newPassword) { + await User.resetPassword(this.id, newPassword); this.password = newPassword; } + } module.exports = User;