Reorganize and improve error responses

This commit is contained in:
rui hildt 2020-05-11 00:03:48 +02:00
parent dd6fa0e63c
commit 79a8a2fb75
8 changed files with 138 additions and 74 deletions

View File

@ -2,10 +2,12 @@ const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const Account = require('../models/accountModel');
const { saltingRounds } = require('../../config/config');
const { authenticate } = require('../../middlewares/authMiddleware');
const Account = require('../models/accountModel');
router.post('/', async (req, res) => {
// TODO : remove if unused
const data = { ...req.body };
const hash = bcrypt.hashSync(data.password, saltingRounds);
data.password = hash;
@ -18,16 +20,15 @@ router.post('/', async (req, res) => {
}
});
router.put('/:id', async (req, res) => {
router.put('/:id', authenticate, async (req, res) => {
const data = { ...req.body };
const id = req.params.id;
if (data.password) {
const hash = bcrypt.hashSync(data.password, 10);
data.password = hash;
}
try {
if (data.password) {
const hash = bcrypt.hashSync(data.password, 10);
data.password = hash;
}
const account = await Account.updateAccount(data, id);
res.status(200).json(...account);
} catch (error) {
@ -38,7 +39,7 @@ router.put('/:id', async (req, res) => {
}
});
router.delete('/:id', async (req, res) => {
router.delete('/:id', authenticate, async (req, res) => {
const id = req.params.id;
try {
@ -54,40 +55,41 @@ router.delete('/:id', async (req, res) => {
}
});
router.get('/:id/meetings', async (req, res) => {
router.get('/:id/meetings', authenticate, async (req, res) => {
const { id } = req.params;
try {
const meetings = await Account.getMeetingsByAccountId(id);
res.status(200).json(meetings);
if (meetings.length == 0) {
res.status(200).json({
message: `There are no meetings for account with id ${id}.`,
});
} else {
res.status(200).json(meetings);
}
} catch (error) {
res.status(500).json({
message: `Couldn't get meetings for account with id ${id}.`,
message: `Failed to fetch meetings with account id ${id}.`,
error,
});
}
});
router.get('/:id', async (req, res) => {
router.get('/:id', authenticate, async (req, res) => {
const id = req.params.id;
try {
const account = await Account.getAccountById(id);
res.status(200).json(account);
} catch (error) {
res.status(500).json({ message: "Account doesn't exist.", error });
}
});
router.get('/:email', async (req, res) => {
const email = req.params.email;
try {
const account = await Account.getAccountByEmail(email);
res.status(200).json(account);
if (typeof account == 'undefined') {
res.status(404).json({
message: `Account with id ${id} doesn't exist.`,
});
} else {
res.status(200).json(account);
}
} catch (error) {
res.status(500).json({
message: `Account with ${email} doesn't exist.`,
message: `Failed to fetch account with id ${id}.`,
error,
});
}

View File

@ -4,7 +4,7 @@ const bcrypt = require('bcryptjs');
const Account = require('../models/accountModel');
const { saltingRounds } = require('../../config/config');
const { generateToken } = require('../../helpers/authJwt');
const { generateToken } = require('../../helpers/generateToken');
router.post('/register', async (req, res) => {
const data = req.body;
@ -40,7 +40,7 @@ router.post('/login', async (req, res) => {
}
} catch (error) {
res.status(401).json({
message: `Invalid Credentials`,
message: `Invalid credentials`,
});
}
});

View File

@ -1,9 +1,10 @@
const express = require('express');
const router = express.Router();
const { authenticate } = require('../../middlewares/authMiddleware');
const Availibility = require('../models/availibilityModel');
router.post('/', async (req, res) => {
router.post('/', authenticate, async (req, res) => {
const data = { ...req.body };
try {
@ -17,7 +18,7 @@ router.post('/', async (req, res) => {
}
});
router.delete('/:id', async (req, res) => {
router.delete('/:id', authenticate, async (req, res) => {
const id = req.params.id;
try {

View File

@ -3,9 +3,10 @@ const router = express.Router();
const bcrypt = require('bcryptjs');
const { v4: uuidv4 } = require('uuid');
const { authenticate } = require('../../middlewares/authMiddleware');
const Meeting = require('../models/meetingModel');
router.post('/', async (req, res) => {
router.post('/', authenticate, async (req, res) => {
id = uuidv4();
data = { id, ...req.body };
@ -18,11 +19,11 @@ router.post('/', async (req, res) => {
const [meeting] = await Meeting.addMeeting(data);
res.status(201).json(meeting);
} catch (error) {
res.status(500).json({ message: 'Failed to add meeting.', error });
res.status(500).json({ message: 'Failed to add new meeting.', error });
}
});
router.put('/:id', async (req, res) => {
router.put('/:id', authenticate, async (req, res) => {
const data = { ...req.body };
const id = req.params.id;
@ -42,7 +43,7 @@ router.put('/:id', async (req, res) => {
}
});
router.delete('/:id', async (req, res) => {
router.delete('/:id', authenticate, async (req, res) => {
const id = req.params.id;
try {
@ -58,47 +59,104 @@ router.delete('/:id', async (req, res) => {
}
});
router.get('/:id', async (req, res) => {
router.get('/:id', authenticate, async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getMeetingById(id);
res.status(200).json(meeting);
if (typeof meeting == 'undefined') {
res.status(404).json({
message: `Meeting with id ${id} could not be found.`,
});
} else {
res.status(200).json(meeting);
}
} catch (error) {
res.status(500).json({ message: "Meeting doesn't exist.", error });
res.status(500).json({
message: `Failed to fetch meeting with id ${id}`,
error,
});
}
});
router.get('/:id/participants', async (req, res) => {
router.get('/:id/participants', authenticate, async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getParticipantsByMeetingId(id);
res.status(200).json(meeting);
const meeting = await Meeting.getMeetingById(id);
if (typeof meeting == 'undefined') {
res.status(404).json({
message: `Meeting with id ${id} could not be found.`,
});
} else {
const participants = await Meeting.getParticipantsByMeetingId(id);
if (participants.length == 0) {
res.status(200).json({
message: `There are no participants for meeting with id ${id}.`,
});
} else {
res.status(200).json(participants);
}
}
} catch (error) {
res.status(500).json({ message: "Meeting doesn't exist.", error });
res.status(500).json({
message: `Failed to fetch participants for meeting with id ${id}.`,
error,
});
}
});
router.get('/:id/possible-dates', async (req, res) => {
router.get('/:id/possible-dates', authenticate, async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getPossibleDatesByMeetingId(id);
res.status(200).json(meeting);
const meeting = await Meeting.getMeetingById(id);
if (typeof meeting == 'undefined') {
res.status(404).json({
message: `Meeting with id ${id} could not be found.`,
});
} else {
const possibleDates = await Meeting.getPossibleDatesByMeetingId(id);
if (possibleDates.length == 0) {
res.status(200).json({
message: `There are no possibles dates for meeting with id ${id}.`,
});
} else {
res.status(200).json(possibleDates);
}
}
} catch (error) {
res.status(500).json({ message: "Meeting doesn't exist.", error });
res.status(500).json({
message: `Failed to fetch possible dates for meeting with id ${id}`,
error,
});
}
});
router.get('/:id/availibility', async (req, res) => {
router.get('/:id/availibility', authenticate, async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getAvailibilityByMeetingId(id);
res.status(200).json(meeting);
const meeting = await Meeting.getMeetingById(id);
if (typeof meeting == 'undefined') {
res.status(404).json({
message: `Meeting with id ${id} could not be found.`,
});
} else {
const availibility = await Meeting.getAvailibilityByMeetingId(id);
if (availibility.length == 0) {
res.status(200).json({
message: `There are no possibles dates for meeting with id ${id}.`,
});
} else {
res.status(200).json(availibility);
}
}
} catch (error) {
res.status(500).json({ message: "Meeting doesn't exist.", error });
res.status(500).json({
message: `Failed to fetch availibility for meeting with id ${id}`,
error,
});
}
});

View File

@ -1,9 +1,10 @@
const express = require('express');
const router = express.Router();
const { authenticate } = require('../../middlewares/authMiddleware');
const Participant = require('../models/participantModel');
router.post('/', async (req, res) => {
router.post('/', authenticate, async (req, res) => {
const data = { ...req.body };
try {
@ -17,7 +18,7 @@ router.post('/', async (req, res) => {
}
});
router.put('/:account_id-:meeting_id', async (req, res) => {
router.put('/:account_id-:meeting_id', authenticate, async (req, res) => {
const data = { ...req.body };
const { account_id, meeting_id } = req.params;
@ -36,7 +37,7 @@ router.put('/:account_id-:meeting_id', async (req, res) => {
}
});
router.delete('/:account_id-:meeting_id', async (req, res) => {
router.delete('/:account_id-:meeting_id', authenticate, async (req, res) => {
const { account_id, meeting_id } = req.params;
try {
@ -49,13 +50,13 @@ router.delete('/:account_id-:meeting_id', async (req, res) => {
});
} catch (error) {
res.status(500).json({
message: 'Failed to delete participant.',
message: `Failed to delete participant with id ${account_id}-${meeting_id}.`,
error,
});
}
});
router.get('/:account_id-:meeting_id', async (req, res) => {
router.get('/:account_id-:meeting_id', authenticate, async (req, res) => {
const { account_id, meeting_id } = req.params;
try {
@ -66,7 +67,7 @@ router.get('/:account_id-:meeting_id', async (req, res) => {
res.status(200).json(participant);
} catch (error) {
res.status(500).json({
message: `Participant with id ${account_id}-${meeting_id} doesn't exist.`,
message: `Failed to get participant with id ${account_id}-${meeting_id}.`,
error,
});
}

View File

@ -1,9 +1,10 @@
const express = require('express');
const router = express.Router();
const { authenticate } = require('../../middlewares/authMiddleware');
const PossibleDate = require('../models/possibleDateModel');
router.post('/', async (req, res) => {
router.post('/', authenticate, async (req, res) => {
const data = { ...req.body };
try {
@ -17,7 +18,7 @@ router.post('/', async (req, res) => {
}
});
router.delete('/:id', async (req, res) => {
router.delete('/:id', authenticate, async (req, res) => {
const id = req.params.id;
try {

17
helpers/generateToken.js Normal file
View File

@ -0,0 +1,17 @@
const jwt = require('jsonwebtoken');
const { jwt_secret } = require('../config/config');
function generateToken(user) {
const payload = {
username: user.username,
email: user.email,
};
const options = {
expiresIn: '30d',
};
return jwt.sign(payload, jwt_secret, options);
}
exports.generateToken = generateToken;

View File

@ -1,24 +1,6 @@
const jwt = require('jsonwebtoken');
const { jwt_secret } = require('../config/config');
module.exports = {
authenticate,
generateToken,
};
function generateToken(user) {
const payload = {
username: user.username,
email: user.email
};
const options = {
expiresIn: '30d',
};
return jwt.sign(payload, jwt_secret, options);
}
function authenticate(req, res, next) {
const token = req.get('Authorization');
@ -34,3 +16,5 @@ function authenticate(req, res, next) {
});
}
}
exports.authenticate = authenticate;