How can I run custom auth logic, like beforeLogin

Before succesfully logging in I need to manipulate some data on the user’s entity, how can I make an authentication lifecycle?

This topic has been created from a Discord post (1249852003195289692) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

in v3 i could just copy paste the auth code and edit anything i wanted before sending over the ctx, how do I do this now? documentations not helping

<@1072591948499664996> how can I modify the login functionality so that I can first make a check to see if the user that is about to login has a certain boolean value on or off before actually sending back the login result

Let me check the knowledge base for you. One moment! :books:


To modify the login functionality in Strapi, you would need to customize the users-permissions plugin and specifically the Auth controller. This is where the check for the existence of a User is made during the login process.

Here’s a general idea of how you could approach this:

  1. Locate the Auth.js file in the users-permissions plugin. The exact location of this file may vary depending on your Strapi setup, but it’s typically found in the controllers directory of the plugin.

  2. In the Auth.js file, find the function that handles user login. This function is typically named login or something similar.

  3. Inside the login function, before the user is authenticated and the login result is sent back, add a check for the specific boolean value on the user object. You can access the user object through the ctx parameter that’s passed into the function.

Here’s a simplified example of what the modified login function might look like:

async function login(ctx) {
  const user = await strapi.plugins['users-permissions'].services.user.fetch({
    id: ctx.state.user.id,
  });

  if (user.yourBooleanAttribute) {
    // Continue with the login process if the boolean value is true
  } else {
    // Return an error or redirect the user if the boolean value is false
  }
}

Please replace yourBooleanAttribute with the actual name of the boolean attribute you want to check on the user object.

Remember that this is a simplified example and the actual code may be more complex depending on your specific requirements and the existing code in the Auth.js file.

For more information on customizing controllers in Strapi, you can refer to the official Strapi documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#controllers).

Please note that modifying the core plugins of Strapi can lead to issues when upgrading Strapi to a new version in the future. Always make sure to thoroughly test your changes and consider the potential impact on future upgrades.

Source: Github Issue (closed): Making another model authenticatable > Comments (https://github.com/strapi/strapi/issues/2015)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

<@1072591948499664996> I cant find it, make sure its strapi v4, how do I extend the login functionality

Let me check the knowledge base for you. One moment! :books:


In Strapi v4, the process of extending the login functionality involves creating a custom function in the ./src/api/[plugin]/controllers/auth.js file. However, the exact details on how to do this are not provided in the given knowledge sources.

It’s important to note that extending the core functionalities of Strapi should be done with caution as it can lead to issues when upgrading Strapi to a new version in the future. Always make sure to thoroughly test your changes and consider the potential impact on future upgrades.

For more detailed instructions, you may want to refer to the official Strapi documentation or community resources. Unfortunately, the provided knowledge sources do not contain specific instructions or code examples for extending the login functionality in Strapi v4.

If you’re facing issues or need further assistance, consider reaching out to the Strapi community through their official forum (https://forum.strapi.io/) or GitHub (https://github.com/strapi/strapi).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

I finally found it but holy @#$% guys please hire me to fix your documentation this is absolutely horrible

Wouldn’t find it without this guy’s post

What’s the plugin type? what are the plugin.controllers.*? bruh

absolutely horrible
Come on man, you can be a little more polite, you’ve been here long enough to know the rules

We aren’t that strict on them, basically “don’t be a dick” and I’d say this qualifies

I’m sorry man but I was giga frustrated, but here’s some real feedback: Please finish the documentation before making major version changes like v4 to v5, I think I’ve said it in the recent survey

I remember the way we did extending the auth/callback in v3, now it changed in v4 without any docs, and I don’t know what’s going to happen in v5 with this same logic, it’s like the versions change too fast

We have technical writer to write documentation for 30 engineers, he is doing the best he can and we can’t just stall development for documentation or our company is just going to collapse.

Our docs are open source, when is the last time you opened a PR to fix the docs?

Your hypocrisy and entitled attitude aren’t welcome. We give our software and documentation for free and we are doing the best we can but we are human.