Tutorial: LoopBack 4 API with JWT Authentication

Afonso Antunes
2 min readApr 14, 2021

Hi people!!

In this tutorial, I will show you how to build an API using the JWT authentication in LoopBack 4.

1st step: Install node.js the Todo example and install the JWT authentication extension

npm init
lb4 example todo
cd loopback4-example-todo
npm i --save @loopback/authentication @loopback/authentication-jwt

2nd step: Create the Controller for login

3rd step: Associate the JWT component in the application placing the code in the application.ts file

4th step: Add endpoints in the user.controller.ts file

5th step: Protect the Todo APIs in the todo.controller.ts file

6th step: Execute the API using the following server

The output should be as follows:

7th step: Sign up using the POST/signup API

8th step: Login using the POST /users/login API, that is, copy the respective ID that is found in the response body

Last step: Go to the top of the API Explorer, click the “Authorize” button, paste the ID and click Authorize

And this is it!!