learning-project-01 / bookstore-app

3 stars 2 forks source link

login to application and use x-auth-token #71

Closed rkpatra201 closed 9 months ago

rkpatra201 commented 9 months ago

If you have finished your signup, then

  1. Obtain token: Login using user email and password.
  2. Use token: Use token with header X-AUTH-TOKEN

Obtain token: Login using user email and password.

image

curl --location 'localhost:8080/user/login' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email":"abcd@email.com",
    "password": "abc@123"
}'

From the above postman we can see below reponse:

{
    "type": "ENCODED",
    "value": "YzJjMDgzODMtNWE2Ni00YjY1LTkzMmYtYWE2ZTllN2UxNjkw"
}

copy the value i.e. : YzJjMDgzODMtNWE2Ni00YjY1LTkzMmYtYWE2ZTllN2UxNjkw

Use token: Use token with header X-AUTH-TOKEN

We will use above copied token in subsequent request as shown below.

For example, I want to access the endpoint: GET http://localhost:8080/cart in postman

So in postman under headers section I will add a header called X-AUTH-TOKEN and value would above copied value.

image

curl --location 'localhost:8080/cart' \
--header 'X-AUTH-TOKEN: YzJjMDgzODMtNWE2Ni00YjY1LTkzMmYtYWE2ZTllN2UxNjkw'

Now if I dont pass token then I will get 401 UNAUTHORIZED reponse, as shown below.

See I have disabled the X-AUTH-TOKEN header in the screenshot.

image

Hence always pass the token after doing login, otherwise it will give 401 UNAUTHORIZED error.