reset password API complete

This commit is contained in:
xfarrow
2024-03-25 15:39:04 +01:00
parent c06869d3b4
commit dbdc9f78c5
3 changed files with 68 additions and 12 deletions

View File

@ -139,7 +139,7 @@ async function remove(personId) {
async function confirmActivation(personId) {
await knex.transaction(async (tr) => {
await knex('Person')
await tr('Person')
.where('id', personId)
.update({
enabled: true
@ -149,7 +149,6 @@ async function confirmActivation(personId) {
.where('person_id', personId)
.del();
});
}
// Exporting a function

View File

@ -13,14 +13,47 @@
const knex = require('../utils/knex_config');
async function add(email, secret){
async function add(email, secret) {
await knex('RequestResetPassword')
.insert({
email,
.insert({
email,
secret
});
}
async function findBySecret(secret) {
return await knex('RequestResetPassword').where({
secret
}).first();
}
/**
* Given a secret and a new password, update the Peron's personal password
*
* @param {*} password The new (hashed) password
* @param {*} secret The secret received via e-mail (table RequestResetPassword)
* @returns
*/
async function resetPassword(password, secret) {
const request = await findBySecret(secret);
if (!request) {
return;
}
await knex.transaction(async tr => {
await tr('Person').where({
email: request.email
}).update({
password
});
await tr('RequestResetPassword').where({
email
}).del();
});
}
module.exports = {
add
add,
findBySecret,
resetPassword
}