curl --request POST \
--url https://api.example.com/auth/login \
--header 'Content-Type: application/json' \
--data '
{
"email": "<string>",
"password": "<string>"
}
'{
"message": "<string>",
"data": {
"email": "<string>",
"first_name": "<string>",
"role": "<string>",
"token": "<string>"
}
}Authenticate a user and receive a JWT token
curl --request POST \
--url https://api.example.com/auth/login \
--header 'Content-Type: application/json' \
--data '
{
"email": "<string>",
"password": "<string>"
}
'{
"message": "<string>",
"data": {
"email": "<string>",
"first_name": "<string>",
"role": "<string>",
"token": "<string>"
}
}curl -X POST https://api.marsai.com/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "producer@example.com",
"password": "securePassword123"
}'
const response = await fetch('https://api.marsai.com/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: 'producer@example.com',
password: 'securePassword123'
})
});
const data = await response.json();
console.log(data.data.token);
Show data properties
id: User ID (id_user)role: User roleexpiresIn: Token expiration time (default 1 hour){
"message": "Connexion réussie",
"data": {
"email": "producer@example.com",
"first_name": "John",
"role": "PRODUCER",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
{
"error": "Identifiants invalides"
}
{
"error": "Erreur serveur"
}
User.findOne({ where: { email } }))comparePassword(password, user.password))jsonwebtoken libraryback/src/controllers/AuthController.js:20-60
function login(req, res) {
const { email, password } = req.body;
User.findOne({ where: { email } }).then((user) => {
if (!user) {
return res.status(401).json({ error: "Identifiants invalides" });
}
comparePassword(password, user.password).then((isMatch) => {
if (!isMatch) {
return res.status(401).json({ error: "Identifiants invalides" });
}
const token = jwt.sign(
{ id: user.id_user, role: user.role },
process.env.JWT_SECRET,
{ expiresIn: process.env.JWT_EXPIRES_IN || "1h" }
);
const responseData = {
message: "Connexion réussie",
data: {
email: user.email,
first_name: user.first_name,
role: user.role,
token,
}
};
return res.status(200).json(responseData);
});
});
}
curl -X GET https://api.marsai.com/user/profile \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."