Guards
Guards in microservices serve the same purpose as they do in normal APIs. They determine if a specific microservice handler should handle a request. Up to now, we have used guards to protect API handlers from unauthorized access. We should do the same thing to our microservice handlers. Although in our application, our microservice handler is only called from our already protected API handler, we should never assume that will always be the case.
@
Injectable
()
export
class
RpcCheckLoggedInUserGuard
implements
CanActivate
{
canActivate
(
context
:ExecutionContext
)
:
boolean
|
Promise
<
boolean
>
|
Observable
<
boolean
>
{
const
data
=
context
.
switchToRpc
().
getData
();
return
Number
(
data
.
userId
)
===
data
.
user
.
id
;
}
}
The new guard looks exactly like the API CheckLoggedInUserGuard
guard. The difference is in the parameters that are passed to the canActivate
method. Since this guard is being executed in the context of a microservice, it will be given a microservice...