Guards
A guard is a class annotated with the @Injectable()
decorator, which implements the CanActivate
interface.
Guards have a single responsibility. They determine whether a given request will be handled by the message handler or not, depending on certain conditions (like permissions, roles, ACLs, etc.) present at run-time.
Author guard
1@Injectable()
2export class IsAuthorGuard implements CanActivate {
3 canActivate(
4 msg: proto.IWebMessageInfo,
5 socket: WASocket
6 ): boolean | Promise<boolean> {
7 return msg.key.fromMe;
8 }
9}
Every guard must implement a canActivate()
function. This function should return a boolean, indicating whether the current message handler is allowed or not. It can return the response either synchronously or asynchronously (via a Promise
). Fastwa uses the return value to control the next action:
- if it returns
true
, the message will be processed. - if it returns
false
, Fastwa will deny the message.
Binding guards
Guards are method-scoped. Below is an example of a method-scoped guard using the @UseGuards()
decorator. This decorator may take a single argument, or a commma-separated list of arguments. This lets you easily apply the appropriate set of guards with one declaration.
1@Command('ping')
2@UseGuards(IsAuthorGuard)
3ping() {
4 return this.appService.getPing();
5}
Good to know: The
@UseGuards()
decorator is imported from@fastwa/common
package.