Authenticate a user and receive a JWT token
curl --request POST \
--url https://api.example.com/api/login \
--header 'Content-Type: <content-type>' \
--header 'x-api-key: <x-api-key>' \
--data '
{
"email": "<string>",
"password": "<string>"
}
'{
"status": "<string>",
"message": "<string>",
"data": {
"token": "<string>"
}
}Documentation Index
Fetch the complete documentation index at: https://mintlify.com/memoowi/e-comm-api-demo-2/llms.txt
Use this file to discover all available pages before exploring further.
POST http://localhost:5000/api/login
application/jsonShow data
export const login = async (req, res) => {
const { email, password } = req.body;
if (!email || !password) {
return errorResponse({
res,
statusCode: 400,
message: "All fields are required",
});
}
try {
const [rows] = await db.execute("SELECT * FROM users WHERE email = ?", [
email,
]);
if (rows.length === 0) {
return errorResponse({ res, statusCode: 404, message: "User not found" });
}
const user = rows[0];
const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) {
return errorResponse({
res,
statusCode: 401,
message: "Incorrect Password",
});
}
const token = jwt.sign(
{ id: user.id, email: user.email, name: user.name, profile_photo: user.profile_photo },
process.env.JWT_SECRET,
{ expiresIn: "8h" }
);
successResponse({
res,
statusCode: 200,
message: "Login successful",
data: { token },
});
} catch (err) {
errorResponse({ res, statusCode: 500, message: "Internal Server Error" });
}
};
curl -X POST http://localhost:5000/api/login \
-H "x-api-key: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"email": "john@example.com",
"password": "securepassword123"
}'
{
"status": "OK",
"message": "Login successful",
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
{
"status": "ERROR",
"message": "All fields are required",
"data": null
}
{
"status": "ERROR",
"message": "Incorrect Password",
"data": null
}
{
"status": "ERROR",
"message": "Invalid API Key",
"data": null
}
{
"status": "ERROR",
"message": "User not found",
"data": null
}
{
"status": "ERROR",
"message": "Internal Server Error",
"data": null
}
Authorization: Bearer <token>
{
"id": 1,
"email": "john@example.com",
"name": "John Doe",
"profile_photo": "path/to/photo.jpg",
"iat": 1234567890,
"exp": 1234596690
}
bcrypt.compare() for secure validationx-api-key headercurl --request POST \
--url https://api.example.com/api/login \
--header 'Content-Type: <content-type>' \
--header 'x-api-key: <x-api-key>' \
--data '
{
"email": "<string>",
"password": "<string>"
}
'{
"status": "<string>",
"message": "<string>",
"data": {
"token": "<string>"
}
}