Activate Person by its activation link

This commit is contained in:
xfarrow 2024-03-04 10:37:54 +01:00
parent 5c76a3b061
commit c1ba83d52e
4 changed files with 68 additions and 5 deletions

View File

@ -56,6 +56,7 @@ publicRoutes.post('/register', personRoutes.registerPerson);
publicRoutes.post('/login', personRoutes.login); publicRoutes.post('/login', personRoutes.login);
publicRoutes.get('/person/:id/details', personRoutes.getPerson); publicRoutes.get('/person/:id/details', personRoutes.getPerson);
publicRoutes.get('/organization/:id', organizationRoutes.getOrganization); publicRoutes.get('/organization/:id', organizationRoutes.getOrganization);
publicRoutes.post('/person/activation', personRoutes.enablePersonByActivationLink);
const protectedRoutes = express.Router(); const protectedRoutes = express.Router();
protectedRoutes.use(jwtUtils.verifyToken); protectedRoutes.use(jwtUtils.verifyToken);

View File

@ -0,0 +1,33 @@
/*
This code is part of Blink
licensed under GPLv3
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
const knex = require('../utils/knex_config');
/**
* Get a Person's ID by its activation identifier
* @param {*} identifier
* @returns
*/
async function getPersonIdByIdentifier (identifier){
const tuple = await knex('ActivationLink')
.where('identifier', identifier)
.first();
if(tuple){
return tuple.person_id;
}
return null;
}
module.exports = {
getPersonIdByIdentifier
};

View File

@ -128,12 +128,18 @@ async function updatePerson (person, person_id) {
* Deletes a Person specified by its database id. * Deletes a Person specified by its database id.
* @param {*} person_id * @param {*} person_id
*/ */
async function deletePerson (person_id) { async function deletePerson (personId) {
await knex('Person') await knex('Person')
.where({ id: person_id }) .where({ id: personId })
.del(); .del();
} }
async function enablePerson (personId) {
await knex('Person')
.where('id', personId)
.update({enabled: true});
}
// Exporting a function // Exporting a function
// means making a JavaScript function defined in one // means making a JavaScript function defined in one
// module available for use in another module. // module available for use in another module.
@ -144,5 +150,6 @@ module.exports = {
getPersonByEmailAndPassword, getPersonByEmailAndPassword,
registerPerson, registerPerson,
updatePerson, updatePerson,
deletePerson deletePerson,
enablePerson
}; };

View File

@ -16,7 +16,7 @@ const jwtUtils = require('../utils/middleware_utils');
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const crypto = require('crypto'); const crypto = require('crypto');
const personModel = require('../models/person_model'); const personModel = require('../models/person_model');
const activationModel = require('../models/activation_model');
/** /**
* POST Request * POST Request
* *
@ -225,6 +225,27 @@ async function deletePerson (req, res) {
} }
} }
/**
* POST Request
*
* Enable a Person object by its activation identifier
*
* Required field(s): identifier
*/
async function enablePersonByActivationLink(req, res){
try {
const personId = await activationModel.getPersonIdByIdentifier(req.body.identifier);
if(!personId){
return res.status(401).json({error: 'Activation Link either not valid or expired'});
}
await personModel.enablePerson(personId);
return res.status(200).json({ success: true });
} catch (error) {
console.error(`Error in function ${enablePersonByActivationLink.name}: ${error}`);
return res.status(500).json({ error: 'Internal server error' });
}
}
// Exporting a function // Exporting a function
// means making a JavaScript function defined in one // means making a JavaScript function defined in one
// module available for use in another module. // module available for use in another module.
@ -234,5 +255,6 @@ module.exports = {
getPerson, getPerson,
getMyself, getMyself,
updatePerson, updatePerson,
deletePerson deletePerson,
enablePersonByActivationLink
}; };