Adding user authentication
Adding user authentication is one of the main requirements in developing an application. This feature allows us to restrict pages and features from unauthorized users. We can achieve user authentication in different ways, and one way to implement this is by providing a login page that will ask for credentials.
Let’s have a look at the step-by-step process of implementing the authentication feature.
The authentication API
Let us first recap the authentication API we created in our Spring Boot project. The endpoints for authentication are as follows:
{BASE_URL}/authenticate
: The main endpoint for authentication accepts an object with email and password fields and returns a valid JWT that will be used for calling endpoints. The following is an example response object of the endpoint:// valid JWT
{
"token": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0QGdtYWl sLmNvbSIsImlhdCI6MTY1OTQyODk2MSwiZXhwIjoxNjU5NDY0OTYxfQ...