Maxence
Maxence

Reputation: 2339

Customizing flash messages per success/failure for Devise

I am trying to generate different looking flash messages depending on a user sessions successful or failed login. (Mainly by changing the jpeg image)

I have a partial view handling the flash messages and check for the key and then display a different image:

_flash_message.html.erb

<% if flash.present? %>
    <% case flash.first[0] %>

<% when "devise_deconnexion" %>

        <div id="flash-message">
            <div id="image">
                <%= image_tag "deconnecte.svg" %>
            </div>
            <div id="affiche">
                <div id="message">
                    <h1>Succès</h1>
                    <h2><%= flash.first[1] %></h2>
                </div>
                <div id="check" style="background-color: #00e691;">
                    <%= image_tag "check_blanc.svg" %>
                </div>
            </div>
        </div>

...

In the above bit I check if key is matching "devise_deconnexion" string in order to show a different image in the flash message.

I have been able to tweak this by generating devise sessions controller for each Devise model and changing as per below:

sessions_controller.rb

def destroy
    #   super    
    signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
    set_flash_message! :devise_deconnexion, :signed_out if signed_out
    yield if block_given?
    respond_to_on_destroy
  end

It works great.

Though I have hard time changing the image in the case a user type a wrong password. I don't know where to modify the flash key.

Here is the Devise sessions#create code taken from GitHub:

 # POST /resource/sign_in
  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource_name, resource)
    yield resource if block_given?
    respond_with resource, location: after_sign_in_path_for(resource)
  end

I only see the :notice key which is used with :signed_in message.

I can't see where the flash message for "wrong password or username" is triggered (though I indeed get a flash message when a wrong password is entered)

Upvotes: 1

Views: 1424

Answers (1)

chumakoff
chumakoff

Reputation: 7044

When a user types a wrong password the code execution in the controller stops at this line:

self.resource = warden.authenticate!(auth_options)

After that the failed request is processed by so called 'Devise Failure application'.

https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb

You can replace that Failure application with your own which is customized.

1) Create the custom failure app:

class CustomFailureApp < Devise::FailureApp
    # your custom code goes here ....
end

2) Tell Devise to use your custom failure app

# initializers/devise.rb
Devise.setup do |config|
  config.warden do |manager|
    manager.failure_app = CustomFailureApp
  end
end

How to customize the Devise's Failure application to achieve your goals is for you to figure out. Good luck!

Upvotes: 1

Related Questions