Authenticated request

In this guide you will see how you can request the API as an authenticated user.

Login as a Reader#

Here is the API route for the authentication /auth/local.

You have to request it in POST.

import axios from 'axios';
const { data } = await axios.post('https://api-v1.cloudgres.com/auth/local', {
identifier: '***EMAIL***',
password: '***PASSWORD***',
});
console.log(data);

The API response contains the user's JWT in the jwt key.

{
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTc2OTM4MTUwLCJleHAiOjE1Nzk1MzAxNTB9.UgsjjXkAZ-anD257BF7y1hbjuY3ogNceKfTAQtzDEsU",
"user": {
"id": 1,
"username": "reader",
...
}
}

You will have to store this JWT;

You should use the JWT in the request to say that you can access to data

import axios from 'axios';
const { data } = await axios.get('https://api-v1.cloudgres.com/xxxxx', {
headers: {
Authorization:
'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTc2OTM4MTUwLCJleHAiOjE1Nzk1MzAxNTB9.UgsjjXkAZ-anD257BF7y1hbjuY3ogNceKfTAQtzDEsU',
},
});
console.log(data);