Skip to content

Commit

Permalink
feat(middleware): mimic GET behavior on /api/v1/sessions and log …
Browse files Browse the repository at this point in the history
…requests
  • Loading branch information
filipedeschamps committed Aug 16, 2022
1 parent daafb3d commit 48374f5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
3 changes: 2 additions & 1 deletion errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ export class ForbiddenError extends BaseError {
}

export class TooManyRequestsError extends BaseError {
constructor({ message, action, stack, errorLocationCode }) {
constructor({ message, action, context, stack, errorLocationCode }) {
super({
message: message || 'Você realizou muitas requisições recentemente.',
action: action || 'Tente novamente mais tarde ou contate o suporte caso acredite que isso seja um erro.',
statusCode: 429,
context: context,
stack: stack,
errorLocationCode: errorLocationCode,
});
Expand Down
59 changes: 57 additions & 2 deletions pages/api/v1/_responses/rate-limit-reached-sessions.public.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
import { UnauthorizedError } from 'errors/index.js';
import nextConnect from 'next-connect';
import { v4 as uuidV4 } from 'uuid';
import snakeize from 'snakeize';
import logger from 'infra/logger.js';
import controller from 'models/controller.js';
import validator from 'models/validator.js';
import { UnauthorizedError, ForbiddenError, TooManyRequestsError } from 'errors/index.js';

export default async function handler(request, response) {
export default nextConnect({
onError: controller.onErrorHandler,
})
.use(controller.injectRequestMetadata)
.use(logRequest)
.get(getValidationHandler, getHandler)
.post(postValidationHandler, postHandler);

function getValidationHandler(request, response) {
validator(request.cookies, {
session_id: 'required',
});

next();
}

function logRequest(request, response, next) {
const error = new TooManyRequestsError({
context: {
method: request.method,
url: request.url,
body: request.body,
},
});

logger.error(snakeize(error));

next();
}

function getHandler(request, response) {
const error = new ForbiddenError({
message: 'Usuário não pode executar esta operação.',
action: 'Verifique se este usuário possui a feature "read:session".',
requestId: uuidV4(),
errorId: uuidV4(),
errorLocationCode: 'MODEL:AUTHORIZATION:CAN_REQUEST:FEATURE_NOT_FOUND',
});

response.status(error.statusCode).json(snakeize(error));
}

function postValidationHandler(request, response, next) {
validator(request.body, {
email: 'required',
password: 'required',
});

next();
}

async function postHandler(request, response) {
const error = new UnauthorizedError({
message: 'Dados não conferem.',
action: 'Verifique se os dados enviados estão corretos.',
Expand Down
16 changes: 14 additions & 2 deletions pages/api/v1/_responses/rate-limit-reached.public.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import snakeize from 'snakeize';
import logger from 'infra/logger.js';
import { TooManyRequestsError } from 'errors/index.js';

export default function handler(request, response) {
const error = new TooManyRequestsError({});
response.status(error.statusCode).json(error);
const error = new TooManyRequestsError({
context: {
method: request.method,
url: request.url,
body: request.body,
},
});

logger.error(snakeize(error));
delete error.context;

response.status(error.statusCode).json(snakeize(error));
}

0 comments on commit 48374f5

Please sign in to comment.