How to change the default login message?

how to change the default login message?

I get this message from Adonis not finding an email in the user table, but I wanted to customize it, has anyone ever done it?

image

1 Like

Hello, welcome to our community :grinning:
Can you please share your code?

This message is an exception message generated by AdonisJS. You can modify it with try/catch or with exceptions (https://adonisjs.com/docs/4.1/exceptions#_wildcard_handler)

The method I am using to authenticate

  async store({ request, auth, session, response }) {
    const { email, password } = request.all()
    await auth.attempt(email, password)

    return 'Logged in successfully'
  }

And the view looks like this

              @if(hasErrorFor('email'))
                  <span class="alert-text"><strong>Cuidado!</strong> {{ getErrorFor('email') }} </span>
              @endif

I know it is possible to change the message using the messages method of a Validator, but I don’t know what would be the correct key for this message

You can try this (specifically for your case):

async store({ request, auth, session, response }) {
    const { email, password } = request.all()
    try {
         await auth.attempt(email, password)
    } catch (error)
    { 
         throw new Error('Cannot login') //new message
    }

    return 'Logged in successfully'
  }
2 Likes

Hello apparently this will solve my problem
https://adonisjs.com/docs/4.1/validator#_extending_validator

I’m coming now how should I add this to my code

Thanks for helping

1 Like