question: authorizationChecker throws Error: at AuthorizationRequiredError.HttpError [as constructor]
See original GitHub issueI try to implement an authorizationChecker, but it throw the following error when it return false
Error: at AuthorizationRequiredError.HttpError [as constructor] (/src/http-error/HttpError.ts:19:22) at AuthorizationRequiredError.UnauthorizedError [as constructor] (/src/http-error/UnauthorizedError.ts:10:9) at new AuthorizationRequiredError (/src/error/AuthorizationRequiredError.ts:12:9) at handleError_1 (/src/driver/express/ExpressDriver.ts:111:87) at /src/driver/express/ExpressDriver.ts:120:45 at process._tickCallback (internal/process/next_tick.js:68:7)
I reproduce this bug with minimal configuration:
// index.ts
import "reflect-metadata";
import { createExpressServer, Action } from "routing-controllers";
import { DemoController } from "./controllers/DemoController";
createExpressServer({
controllers: [DemoController],
authorizationChecker: async (action: Action, roles: string[]) => {
return false;
}
}).listen(3000);
// controllers/DemoController.ts
import { Controller, Get, Authorized } from "routing-controllers";
@Controller("/")
export class DemoController {
@Authorized()
@Get("/")
getDemo() {
return { demo: "this is a demo" };
}
}
Here is a codesandbox with this issue: https://codesandbox.io/s/routing-controllers-bug-ovdwt?file=/src/index.ts
What am I doing wrong ?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
routing-controllers res.sendFile() notfound error - Stack Overflow
I was try to download files from server using client.I'm using https://www.npmjs.com/package/routing-controllers for routings but unable to ...
Read more >routing-controllers-up - npm Package Health Analysis - Snyk
import { HttpError } from 'routing-controllers'; export class ... then routing-controllers will throw authorization required error.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@zecka, routing-controllers will do that out-of-the-box for you. If you want to handle the messages in a central place, you can disable the
defaultErrorHandler
and configure a specific middleware for error handling, that way, any of the built-ins exceptions such asHttpError
BadRequestError
ForbiddenError
InternalServerError
MethodNotAllowedError
NotAcceptableError
NotFoundError
UnauthorizedError
will always output the format you want:
index.ts
middlewares/ErrorHandler.ts
controllers/DemoController.ts
Will automatically produce something like:
Check in the sandbox: https://codesandbox.io/s/routing-controllers-authorizationchecker-559-3ekl0
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.