|
|
## SHARED
|
|
## SHARED
|
|
|
|
|
|
|
|
Contains the decorators used by all controllers
|
|
|
|
|
|
|
|
### Auth.Guard.ts
|
|
|
|
|
|
|
|
Basic guard for verifying user's tokens. Returns `false` if the authorization header is present or the jwt token is not valid. Halts the request in the controller, which throws an exception for `Http-Error-Filter`. The filter returns an error message for the client.
|
|
|
|
|
|
|
|
The guard also modifies the request by adding the user's information from to token to it. This can be accessed by the [`UserDecorator`](./user#userdecoratorts) which passes it to the service.
|
|
|
|
|
|
|
|
The `validateToken` function is used in quite many files, and it might be smart to define it in it's own file and export it for other files. The function itself splits the token and returns user information from it.
|
|
|
|
|
|
|
|
### Http-Error-Filter.ts
|
|
|
|
|
|
|
|
The global error catcher. Denies any unhandled exceptions from happening in the server. Creates an errorResponse for the user, which contains information on why the request failed. Ideally, you want to use the `Throw HttpException` keyword to trigger error messages. Otherwise the server only returns _Internal Server Error_ which might not be very easy to decipher.
|
|
|
|
|
|
|
|
### Roles.Guard.ts
|
|
|
|
|
|
|
|
An advanced guard for verifying user's role. The valid roles are passed with `@Roles()` decorator. If the roles array is empty for some reason, returns true. Returns `false` if the authorization header is present or the jwt token is not valid, same as in AuthGuard. Requires the game's UUID in the request's params. Uses class-validator to verify that the UUID is valid and throws an exception if it's not.
|
|
|
|
|
|
|
|
RolesGuard also adds the user's information in the request, but also adds the `gameperson` relation of the user. This can be accessed by the [`GamePersonDecorator`](./game#gamepersondecoratorts), which passes it to the service. The object contains the whole gameperson entry with it's relations to the `faction` and `group` entity.
|
|
|
|
|
|
|
|
### States.Guard.ts
|
|
|
|
|
|
|
|
An advanced guard for verifying game's state. The valid states are passed with `@GameStates()` decorator. Requires the game's UUID in the request's params. Uses class-validator to verify that the UUID is valid and throws an exception if it's not. Find the game from the database and compares the states. If they match, the request succeeds. Else, it throws an exception informing the user of the valid states for this request.
|
|
|
|
|
|
|
|
### Validation.Pipe.ts
|
|
|
|
|
|
|
|
The logic for validating the DTO in the request body. Throws an exception if the validation does not pass.
|
|
|
|
|
|
|
|
You can configure the [validation properties](https://github.com/typestack/class-validator#passing-options) in const `errors`. The `forbidNonWhitelisted` option throws an exception if the DTO contains a key that is not specified in the DTO file. |