Reorganize and improve error responses
This commit is contained in:
parent
dd6fa0e63c
commit
79a8a2fb75
@ -2,10 +2,12 @@ const express = require('express');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const bcrypt = require('bcryptjs');
|
const bcrypt = require('bcryptjs');
|
||||||
|
|
||||||
const Account = require('../models/accountModel');
|
|
||||||
const { saltingRounds } = require('../../config/config');
|
const { saltingRounds } = require('../../config/config');
|
||||||
|
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||||
|
const Account = require('../models/accountModel');
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
|
// TODO : remove if unused
|
||||||
const data = { ...req.body };
|
const data = { ...req.body };
|
||||||
const hash = bcrypt.hashSync(data.password, saltingRounds);
|
const hash = bcrypt.hashSync(data.password, saltingRounds);
|
||||||
data.password = hash;
|
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 data = { ...req.body };
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
|
|
||||||
|
try {
|
||||||
if (data.password) {
|
if (data.password) {
|
||||||
const hash = bcrypt.hashSync(data.password, 10);
|
const hash = bcrypt.hashSync(data.password, 10);
|
||||||
data.password = hash;
|
data.password = hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const account = await Account.updateAccount(data, id);
|
const account = await Account.updateAccount(data, id);
|
||||||
res.status(200).json(...account);
|
res.status(200).json(...account);
|
||||||
} catch (error) {
|
} 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;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
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;
|
const { id } = req.params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const meetings = await Account.getMeetingsByAccountId(id);
|
const meetings = await Account.getMeetingsByAccountId(id);
|
||||||
|
if (meetings.length == 0) {
|
||||||
|
res.status(200).json({
|
||||||
|
message: `There are no meetings for account with id ${id}.`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
res.status(200).json(meetings);
|
res.status(200).json(meetings);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
message: `Couldn't get meetings for account with id ${id}.`,
|
message: `Failed to fetch meetings with account id ${id}.`,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/:id', async (req, res) => {
|
router.get('/:id', authenticate, async (req, res) => {
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const account = await Account.getAccountById(id);
|
const account = await Account.getAccountById(id);
|
||||||
res.status(200).json(account);
|
if (typeof account == 'undefined') {
|
||||||
} catch (error) {
|
res.status(404).json({
|
||||||
res.status(500).json({ message: "Account doesn't exist.", error });
|
message: `Account with id ${id} doesn't exist.`,
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
router.get('/:email', async (req, res) => {
|
|
||||||
const email = req.params.email;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const account = await Account.getAccountByEmail(email);
|
|
||||||
res.status(200).json(account);
|
res.status(200).json(account);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
message: `Account with ${email} doesn't exist.`,
|
message: `Failed to fetch account with id ${id}.`,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ const bcrypt = require('bcryptjs');
|
|||||||
|
|
||||||
const Account = require('../models/accountModel');
|
const Account = require('../models/accountModel');
|
||||||
const { saltingRounds } = require('../../config/config');
|
const { saltingRounds } = require('../../config/config');
|
||||||
const { generateToken } = require('../../helpers/authJwt');
|
const { generateToken } = require('../../helpers/generateToken');
|
||||||
|
|
||||||
router.post('/register', async (req, res) => {
|
router.post('/register', async (req, res) => {
|
||||||
const data = req.body;
|
const data = req.body;
|
||||||
@ -40,7 +40,7 @@ router.post('/login', async (req, res) => {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(401).json({
|
res.status(401).json({
|
||||||
message: `Invalid Credentials`,
|
message: `Invalid credentials`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||||
const Availibility = require('../models/availibilityModel');
|
const Availibility = require('../models/availibilityModel');
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', authenticate, async (req, res) => {
|
||||||
const data = { ...req.body };
|
const data = { ...req.body };
|
||||||
|
|
||||||
try {
|
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;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -3,9 +3,10 @@ const router = express.Router();
|
|||||||
const bcrypt = require('bcryptjs');
|
const bcrypt = require('bcryptjs');
|
||||||
const { v4: uuidv4 } = require('uuid');
|
const { v4: uuidv4 } = require('uuid');
|
||||||
|
|
||||||
|
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||||
const Meeting = require('../models/meetingModel');
|
const Meeting = require('../models/meetingModel');
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', authenticate, async (req, res) => {
|
||||||
id = uuidv4();
|
id = uuidv4();
|
||||||
data = { id, ...req.body };
|
data = { id, ...req.body };
|
||||||
|
|
||||||
@ -18,11 +19,11 @@ router.post('/', async (req, res) => {
|
|||||||
const [meeting] = await Meeting.addMeeting(data);
|
const [meeting] = await Meeting.addMeeting(data);
|
||||||
res.status(201).json(meeting);
|
res.status(201).json(meeting);
|
||||||
} catch (error) {
|
} 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 data = { ...req.body };
|
||||||
const id = req.params.id;
|
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;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
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;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const meeting = await Meeting.getMeetingById(id);
|
const meeting = await Meeting.getMeetingById(id);
|
||||||
|
if (typeof meeting == 'undefined') {
|
||||||
|
res.status(404).json({
|
||||||
|
message: `Meeting with id ${id} could not be found.`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
res.status(200).json(meeting);
|
res.status(200).json(meeting);
|
||||||
|
}
|
||||||
} catch (error) {
|
} 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;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const meeting = await Meeting.getParticipantsByMeetingId(id);
|
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 {
|
||||||
|
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) {
|
} 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;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const meeting = await Meeting.getPossibleDatesByMeetingId(id);
|
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 {
|
||||||
|
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) {
|
} 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;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const meeting = await Meeting.getAvailibilityByMeetingId(id);
|
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 {
|
||||||
|
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) {
|
} 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,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||||
const Participant = require('../models/participantModel');
|
const Participant = require('../models/participantModel');
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', authenticate, async (req, res) => {
|
||||||
const data = { ...req.body };
|
const data = { ...req.body };
|
||||||
|
|
||||||
try {
|
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 data = { ...req.body };
|
||||||
const { account_id, meeting_id } = req.params;
|
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;
|
const { account_id, meeting_id } = req.params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -49,13 +50,13 @@ router.delete('/:account_id-:meeting_id', async (req, res) => {
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
message: 'Failed to delete participant.',
|
message: `Failed to delete participant with id ${account_id}-${meeting_id}.`,
|
||||||
error,
|
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;
|
const { account_id, meeting_id } = req.params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -66,7 +67,7 @@ router.get('/:account_id-:meeting_id', async (req, res) => {
|
|||||||
res.status(200).json(participant);
|
res.status(200).json(participant);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({
|
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,
|
error,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||||
const PossibleDate = require('../models/possibleDateModel');
|
const PossibleDate = require('../models/possibleDateModel');
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', authenticate, async (req, res) => {
|
||||||
const data = { ...req.body };
|
const data = { ...req.body };
|
||||||
|
|
||||||
try {
|
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;
|
const id = req.params.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
17
helpers/generateToken.js
Normal file
17
helpers/generateToken.js
Normal 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;
|
@ -1,24 +1,6 @@
|
|||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
const { jwt_secret } = require('../config/config');
|
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) {
|
function authenticate(req, res, next) {
|
||||||
const token = req.get('Authorization');
|
const token = req.get('Authorization');
|
||||||
|
|
||||||
@ -34,3 +16,5 @@ function authenticate(req, res, next) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.authenticate = authenticate;
|
Loading…
Reference in New Issue
Block a user