This example, written in Javascript (Node.js), shows how authentication can be done using a password. It also shows how access to events are revoked as soon as the client is logged out. The server consists of three files:
tickerService.js
- exposes a single resource:
ticker.model
- updates the model every second
- requires a token to access the resource
passwdService.js
- exposes two authentication methods,
passwd.login
andpasswd.logout
login
auth method verifies the password parameter and sets a connection tokenlogout
auth method clears any connection token, by setting it to null
server.js
- starts the tickerService.js and passwdService.js micro-services
- serves
/index.html
which is the example client
- Have NATS Server and Resgate running
- Have node.js installed
Run the following commands:
npm install
npm start
Open the client
http://localhost:8085
Gain access
Log in with the password secret
to set the client's access token and start seeing the ticking counter.
Remove access
Click on the Logout button to clear the client's access token. As the ticker.model
resource requires an access token, Resgate will force the client to unsubscribe to the resource.
Regain access
Logging in again will allow the client to resume getting updates. There might be a slight delay before the updates start, as ResClient will periodically try to resubscribe to resources still being listened to.
Note
This example does not handle disconnects or Resgate restarts.
Look at Client Session example or JWT Authentication example to learn more about session handling.