The decorator.
import { Authorize, Controller, ControllerBase, GET, IHttpRequest, IHttpResponse } from '@egomobile/http-server'
@Controller()
@Authorize(['user']) // requires 'user' role
export default class IndexController extends ControllerBase {
// use global authorize
@GET()
async foo(request: IHttpRequest, response: IHttpResponse) {
// you can access request.authorizedUser with authorized user
}
@GET({
// define custom validator as filter expression
//
// s. https://github.com/m93a/filtrex
// for more information
authorize: 'hasRole("admin") and hasHeader("x-my-header", "my-header-value")',
})
async bar(request: IHttpRequest, response: IHttpResponse) {
// you can access request.authorizedUser with authorized user
}
}
// ...
// your initlaizer script
// ...
app.controllers({
authorize: {
// try find data for an existing user
findAuthorizedUser: async (context) => {
// if there is no matching user, return
// a falsy value, like (null) or (undefined)
return {
roles: roles // an array of roles
}
}
}
})
/// ...
Optional
onValidationFailed: Nilable<AuthorizeValidationFailedHandler>Optional
onValidationFailed: Nilable<AuthorizeValidationFailedHandler>Generated using TypeDoc
Marks a class as controller.