Create custom login route

Hi,

I want to create a custom login route instead of /api/auth/local.
Reason for this is that I want to check if a specific IP tried to login more than 3 times in 10 minutes for safety reasons. After the third time I want to block this IP for 15 minutes so basically I want to do some extra checks. Also creating a middleware for the login process would be nice. I have created a custom route and a controller. I can’t get it work. This is my code in my controller right now:

async login(ctx) {
    try {
      const { identifier, password } = ctx.request.body;
      const ip = ctx.request.ip;
      const authenticatedUser = strapi.plugins['users-permissions'].services.auth.login({
        identifier,
        password,
      });
      return ctx.send({ user: authenticatedUser });
    } catch (error) {
      return ctx.send({ error: 'Authentication failed' }, 401);
    }
  },

Hope someone knows a little bit more about this :smiley:

Can you please share a screenshot of your controller and file structure.

My file structure is like:
/api/authentication/controllers/authentication.js
/api/authentication/routes/routes.js

Below is a screenshot of my controller and routes.js

Okay looks proper.
Did you debug the code or checked what is the error? (Please share the error)

It returns me

{
    "error": "Authentication failed"
}

I am reading a thread here: Extending /auth/local/register endpoint with custom logic - #5 by N_H

Maybe I should give something like this a try?

Your requirement is different by creating a new route here.
I have developed user authentication related APIs custom work Link. Check this and follow if it helps.

Works for me! Thankyou!

1 Like