uptime-kuma/server/model/user.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-08-09 05:34:44 +00:00
const { BeanModel } = require("redbean-node/dist/bean-model");
const passwordHash = require("../password-hash");
const { R } = require("redbean-node");
const jwt = require("jsonwebtoken");
const { shake256, SHAKE256_LENGTH } = require("../util-server");
2021-08-09 05:34:44 +00:00
class User extends BeanModel {
/**
* Reset user password
2022-04-18 07:21:58 +00:00
* Fix #1510, as in the context reset-password.js, there is no auto model mapping. Call this static function instead.
* @param {number} userID ID of user to update
* @param {string} newPassword
2021-08-09 05:34:44 +00:00
* @returns {Promise<void>}
*/
2022-04-18 07:21:58 +00:00
static async resetPassword(userID, newPassword) {
2021-08-09 05:34:44 +00:00
await R.exec("UPDATE `user` SET password = ? WHERE id = ? ", [
passwordHash.generate(newPassword),
2022-04-18 07:21:58 +00:00
userID
2021-08-09 05:34:44 +00:00
]);
2022-04-18 07:21:58 +00:00
}
/**
* Reset this users password
* @param {string} newPassword
2022-04-18 07:21:58 +00:00
* @returns {Promise<void>}
*/
async resetPassword(newPassword) {
await User.resetPassword(this.id, newPassword);
2021-08-09 05:34:44 +00:00
this.password = newPassword;
}
2022-04-18 07:21:58 +00:00
/**
* Create a new JWT for a user
* @param {User} user
* @param {string} jwtSecret
* @return {string}
*/
static createJWT(user, jwtSecret) {
return jwt.sign({
username: user.username,
h: shake256(user.password, SHAKE256_LENGTH),
}, jwtSecret);
}
2021-08-09 05:34:44 +00:00
}
module.exports = User;