const axios = require("axios");
module.exports = {
async index(ctx) {
const { body } = ctx.request;
const hostname = "localhost";
const absoluteURL = `<http://${hostname}:${strapi.config.server.port}`;>
const sanitizeOutput = (user) => {
const {
password,
resetPasswordToken,
confirmationToken,
...sanitizedUser
} = user;
return sanitizedUser;
};
try {
let { data } = await axios.post(`${absoluteURL}/api/auth/local`, body);
const populatedUser = await strapi.entityService.findOne(
"plugin::users-permissions.user",
data.user.id,
{
populate: {
role: {
fields: ["type"],
},
},
}
);
data.user = sanitizeOutput(populatedUser);
if (data && data.jwt) {
ctx.cookies.set("jwt", data.jwt, {
httpOnly: true,
secure: false,
maxAge: 1000 * 60 * 60 * 24 * 14, // 14 Day Age
});
}
} catch (error) {
// handle error
}
},
};
Please note that this is a basic example and you might need to adjust it according to your needs.
Sources:
- Implementing Authenticated API Requests to Strapi (https://strapi.io/blog/implementing-authenticated-api-requests-to-strapi)
- [How to pass JWT token from