Authentication
We have set up our CommentModule
, and now we want to authenticate the user using the token (have a look to the Authentication chapter). In this example we use a mutualised server for the REST API and the Websocket event handlers. So, we will mutualise the authentication token in order to see how to validate the token received after a user has been logged into the application.
It is important to secure the websocket in order to avoid the access of data without logging into the application.
As shown in the previous part, we have used middleware named AuthenticationGatewayMiddleware
. The purpose of this middleware is to get the token from the web socket query
, which is brought with the auth_token
property.
If the token is not provided, the middleware will return a WsException
, otherwise we will use the jsonwebtoken
library
(have a look to the Authentication chapter) to verify the token.
Let’s set up the middleware:
@
Injectable
()
export
class
AuthenticationGatewayMiddleware...