mirror of
https://github.com/xfarrow/blink
synced 2025-02-19 08:30:36 +01:00
Update person_routes.js
This commit is contained in:
parent
58f059be9f
commit
d491b9064c
@ -11,7 +11,7 @@
|
||||
IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
const validator = require('../utils/validators/person_validator');
|
||||
const personValidator = require('../utils/validators/person_validator');
|
||||
const jwtUtils = require('../utils/middleware_utils');
|
||||
const bcrypt = require('bcrypt');
|
||||
const crypto = require('crypto');
|
||||
@ -29,9 +29,8 @@ const express = require('express');
|
||||
* @returns The activationlink identifier
|
||||
*/
|
||||
async function registerPerson(req, res) {
|
||||
|
||||
const errors = validator.validationResult(req);
|
||||
|
||||
try {
|
||||
const errors = personValidator.validationResult(req);
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(400).json({
|
||||
errors: errors.array()
|
||||
@ -50,7 +49,7 @@ async function registerPerson(req, res) {
|
||||
// Hash provided password
|
||||
const hashPasswordPromise = bcrypt.hash(req.body.password, 10);
|
||||
|
||||
try {
|
||||
|
||||
// Check whether e-mail exists already (enforced by database constraints)
|
||||
const existingUser = await personModel.getPersonByEmail(req.body.email);
|
||||
if (existingUser) {
|
||||
@ -91,15 +90,13 @@ async function registerPerson(req, res) {
|
||||
* @returns The token
|
||||
*/
|
||||
async function createTokenByEmailAndPassword(req, res) {
|
||||
|
||||
const errors = validator.validationResult(req);
|
||||
try {
|
||||
const errors = personValidator.validationResult(req);
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(400).json({
|
||||
errors: errors.array()
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const person = await personModel.getPersonByEmailAndPassword(req.body.email, req.body.password);
|
||||
if (person) {
|
||||
const token = jwtUtils.generateToken(person.id);
|
||||
@ -184,8 +181,8 @@ async function getMyself(req, res) {
|
||||
*
|
||||
*/
|
||||
async function updatePerson(req, res) {
|
||||
|
||||
const errors = validator.validationResult(req);
|
||||
try {
|
||||
const errors = personValidator.validationResult(req);
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(400).json({
|
||||
errors: errors.array()
|
||||
@ -247,7 +244,6 @@ async function updatePerson(req, res) {
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await personModel.updatePerson(updatePerson, req.jwt.person_id);
|
||||
return res.status(200).json({
|
||||
success: 'true'
|
||||
@ -293,13 +289,13 @@ async function deletePerson(req, res) {
|
||||
* Required field(s): q (identifier)
|
||||
*/
|
||||
async function confirmActivation(req, res) {
|
||||
const errors = validator.validationResult(req);
|
||||
try {
|
||||
const errors = personValidator.validationResult(req);
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(400).json({
|
||||
errors: errors.array()
|
||||
});
|
||||
}
|
||||
try {
|
||||
const personId = await activationModel.getPersonIdByIdentifier(req.query.q);
|
||||
if (!personId) {
|
||||
return res.status(401).json({
|
||||
@ -319,15 +315,15 @@ async function confirmActivation(req, res) {
|
||||
}
|
||||
|
||||
const publicRoutes = express.Router(); // Routes not requiring token
|
||||
publicRoutes.post('/persons', validator.registerValidator, registerPerson);
|
||||
publicRoutes.post('/persons/me/token', validator.getTokenValidator, createTokenByEmailAndPassword);
|
||||
publicRoutes.post('/persons', personValidator.registerValidator, registerPerson);
|
||||
publicRoutes.post('/persons/me/token', personValidator.getTokenValidator, createTokenByEmailAndPassword);
|
||||
publicRoutes.get('/persons/:id/details', getPerson);
|
||||
publicRoutes.get('/persons/me/activation', validator.confirmActivationValidator, confirmActivation);
|
||||
publicRoutes.get('/persons/me/activation', personValidator.confirmActivationValidator, confirmActivation);
|
||||
|
||||
const protectedRoutes = express.Router(); // Routes requiring token
|
||||
protectedRoutes.use(jwtUtils.verifyToken);
|
||||
protectedRoutes.get('/persons/me', getMyself);
|
||||
protectedRoutes.patch('/persons/me', validator.updatePersonValidator, updatePerson);
|
||||
protectedRoutes.patch('/persons/me', personValidator.updatePersonValidator, updatePerson);
|
||||
protectedRoutes.delete('/persons/me', deletePerson);
|
||||
|
||||
// Exporting a function
|
||||
|
Loading…
x
Reference in New Issue
Block a user