Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize devise flash messages based on model validations

I am using Devise for authentication in my rails app. There are two devise models in the app, admin and customer.

Customer model uses mobile_number for authentication instead of the usual email_id field. However, the admin field uses email_id for authentication.

Now, the issue I am facing is related to the default flash messages shown by devise.

Consider, when a user enters invalid login credentials, the message is

'Invalid email or password'

I want to customize this message based on models. So, if admin enters incorrect credentials, message is:

'Invalid email or password'

If customer enters the same, it is:

'Invalid mobile number or password'
like image 262
Nerve Avatar asked May 20 '26 04:05

Nerve


1 Answers

Currently Devise gem supports different kinds of locale files like for English, Arabic, Chinese etc for showing all flash messages related to Devise authentication in different languages.

Refer this link. Choose the English version. In that you could see when you need to show invalid email or password flash message. So the solution is,

en:
  devise:
    failure:
      customer:
        invalid: 'Welcome user, you are signed in.'
        not_found_in_database: 'Welcome user, you are signed in.'
      admin:
        invalid: 'Invalid mobile number or password'
        not_found_in_database: 'Invalid mobile number or password'

Hope this helps :)

like image 159
Rajesh Omanakuttan Avatar answered May 21 '26 17:05

Rajesh Omanakuttan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!