diff --git a/backend/apis/nodejs/api.js b/backend/apis/nodejs/api.js index 01c347e..2ebd90e 100644 --- a/backend/apis/nodejs/api.js +++ b/backend/apis/nodejs/api.js @@ -35,6 +35,7 @@ publicRoutes.post('/login', apiController.login); const protectedRoutes = express.Router(); protectedRoutes.use(apiController.verifyToken); protectedRoutes.get('/person/:id', apiController.getPerson); +protectedRoutes.get('/person/myself', apiController.getMyself); protectedRoutes.put('/person/:id', apiController.updatePerson); protectedRoutes.delete('/person/delete', apiController.deletePerson); protectedRoutes.post('/organization/admin', apiController.addOrganizationAdmin); diff --git a/backend/apis/nodejs/api_controller.js b/backend/apis/nodejs/api_controller.js index ba053ca..18f28b9 100644 --- a/backend/apis/nodejs/api_controller.js +++ b/backend/apis/nodejs/api_controller.js @@ -140,7 +140,7 @@ async function getPerson(req, res){ try { const user = await knex('Person') .select('*') - .where({ id: req.params.id, enabled: true }) + .where({ id: req.params.id }) .first(); if(user){ @@ -158,6 +158,33 @@ async function getPerson(req, res){ } } +/** + * + * GET Request + * + * Get myself, from the JWT token + * + * @returns Person's details + */ +async function getMyself(req, res){ + try{ + const person = await knex('Person') + .select('*') + .where({ id: req.jwt.person_id }) + .first(); + + if(person){ + delete person['password']; + return res.status(200).send(person); + } + return res.status(404).json({error: "Not found"}); + } + catch (error){ + console.log("Error while getting myself: " + error); + return res.status(500).json({error : "Internal server error"}); + } +} + /** * PUT request * @@ -243,6 +270,8 @@ async function deletePerson(req, res) { .where({id : req.jwt.person_id}) .del(); return res.status(200).json({success: true}); + + // TODO: Delete Organization if this user was its only administrator } catch (error) { console.log("Error deleting a Person: " + error); @@ -687,6 +716,7 @@ module.exports = { registerPerson, login, getPerson, + getMyself, updatePerson, deletePerson, verifyToken,