Skip to main content

POST /api/login

Authenticates a user and returns a JWT token.

Endpoint​

POST /api/login

Headers​

HeaderValueRequired
Content-Typeapplication/jsonYes

Request Body​

{
"email": "string",
"password": "string"
}
FieldTypeRequiredDescription
emailstringYesUser email address
passwordstringYesPassword

Success Response​

Code: 200 OK

{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...",
"refresh_token": "d1f2a3b4c5e6f7g8h9i0j1k2l3m4n5o6",
"user": {
"id": "0f8fad5b-d9cb-469f-a165-70867728950e",
"email": "agent@example.org",
"roles": ["ROLE_USER"],
"roleLabel": "Spécialiste Transferts Monétaires",
"nom": "Exemple",
"prenom": "Agent",
"totpEnabled": false,
"passwordExpired": false,
"mustChangePassword": false,
"passwordExpiresIn": 75,
"passwordWarning": null,
"region": null
}
}
FieldTypeDescription
tokenstringJWT access token (lifetime: 1 hour)
refresh_tokenstringRefresh token (lifetime: 30 days, renewed on every refresh)
userobjectLogged-in user; their permissions are then read with GET /api/me/permissions

Besides the body, the response sets three cookies: BEARER (JWT, httpOnly), REFRESH_TOKEN (httpOnly) and XSRF-TOKEN (readable by the browser). A client authenticating by cookie must copy the XSRF-TOKEN value into the X-CSRF-TOKEN header of every POST, PUT, PATCH or DELETE request; a client sending the Authorization: Bearer header without cookies is not affected.

Two-factor authentication (2FA)​

If the user has enabled 2FA, login does not yet issue a usable token:

{
"requires2FA": true,
"message": "Verification 2FA requise",
"user": { "id": "…", "email": "agent@example.org", "totpEnabled": true }
}

The client then sends the 6-digit code to POST /api/2fa/verify, which issues the JWT and the refresh token. The other 2FA routes (/api/2fa/setup, enable, disable, status, login-history) manage activation from the profile.

Error Responses​

401 Unauthorized - Invalid credentials​

{
"code": 401,
"message": "Invalid credentials."
}

400 Bad Request - Missing data​

{
"code": 400,
"message": "The key \"email\" must be provided."
}

403 Forbidden - Account disabled​

{
"code": 403,
"message": "Account is disabled."
}

Examples​

cURL​

curl -X POST https://sig.example.com/api/login \
-H "Content-Type: application/json" \
-d '{
"email": "admin@pch-sig.sn",
"password": "<MOT_DE_PASSE_ADMIN>"
}'

JavaScript​

const login = async (email, password) => {
const response = await fetch('https://sig.example.com/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email, password })
});

if (!response.ok) {
throw new Error('Authentication failed');
}

const data = await response.json();

// Store the token
localStorage.setItem('token', data.token);
localStorage.setItem('refresh_token', data.refresh_token);

return data;
};

Python​

import requests

response = requests.post(
'https://sig.example.com/api/login',
json={
'email': 'admin@pch-sig.sn',
'password': '<MOT_DE_PASSE_ADMIN>'
}
)

if response.status_code == 200:
data = response.json()
token = data['token']
print(f"Connected as {data['user']['email']}")
else:
print(f"Error: {response.json()['message']}")

Using the token​

Once the token is obtained, include it in the Authorization header for subsequent requests:

curl https://sig.example.com/api/menages \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9..."

JWT Token Structure​

The token contains the following information (decoded payload):

{
"iat": 1705312345,
"exp": 1705315945,
"roles": ["ROLE_ADMIN"],
"email": "admin@pch-sig.sn",
"user_id": 1
}
FieldDescription
iatCreation timestamp
expExpiration timestamp
rolesUser roles
emailUser email
user_idUser ID

Notes​

  • The token expires after 1 hour
  • Use the refresh_token to obtain a new token without re-entering credentials
  • Never store the token in an insecure location
  • In production, always use HTTPS