I am not sure what the best practice is to reach my goal:
In my registration process I have a checkbox âregister to newsletterâ. When a new user ticks this box I want to do some API call, after successful registration to an external service to put the user on my mailing list.
So basically I want to hook into the logic of /auth/local/register. I do not want to touch the core logic, just want to execute some custom code after its done.
Is it best to override the register controller? And when yes, how?
Should I create a custom middleware for this? And again: When yes, how?
I would appreciate some feedback with code example or link to a fitting reference.
Hi @Konstantin_Cube
I solved my case by creating a custom middleware. To do so do the following:
create a file with a name by your choice in src/middlewares/yourfile.js
In this file: Hook into Account creation with some code in the created file like this:
module.exports = () => {
return async (ctx, next) => {
await next();
// only if path was register with newsletter param and it was successfull. Then we will put user in the mailing list.
if (ctx.request.url === '/api/auth/local/register?newsletter=true' && ctx.response.status === 200) {
const email = ctx.response.body.user.email;
const name = ctx.response.body.user.nickname;
// we do NOT await this. it has nothing to with account creation and should not block it. just a side effect.
registerToNewsletter(email, name);
}
};
};