简体   繁体   中英

devise overriding sessions controller

I want to add a message when user tries to sign in and it's not confirmed, I want to display this message in notice section, basically devise needs to provide this message to us but I don't see any message in this cases. So I decided to add it manually from sessions controller here is my code:

class SessionsController < Devise::SessionsController

  def new
    super
  end

  def create
    user = User.find_by_email(params[:user][:email])

    self.resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in) if is_flashing_format?
    sign_in(resource_name, resource)

    if user.confirmed_at.nil?
      flash[:notice] = "my message here"
    end
    yield resource if block_given?

    respond_with resource, location: after_sign_in_path_for(resource)
  end

end

the problem is flash[:notice] is empty after action is executed, in console I have

Started POST "/users/sign_in" for 127.0.0.1 at 2014-11-18 15:07:21 +0200
Processing by SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xC86tz4kZjcSMqXOL/+qpwlh5VlSbnsvLj93N5jb3NI=", "user"=>{"email"=>"pers.maki.5@gmail.com", "password"=>"[FILTERED]"}, "commit"=>"Sign in"}
   (0.2ms)  SELECT COUNT(*) FROM "landing_page_reports" 
  LandingPageReport Load (0.2ms)  SELECT "landing_page_reports".* FROM "landing_page_reports" ORDER BY "landing_page_reports"."id" DESC LIMIT 1
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'pers.maki.5@gmail.com' LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'pers.maki.5@gmail.com' LIMIT 1
   (0.1ms)  begin transaction
   (0.2ms)  commit transaction
Completed 401 Unauthorized in 193ms

how can I display this message?

More simple way to do what are asking about is to add before_filter :confirmation_notice in you application_controller and check whether current_user.confirmed? or not.

ex ::

before_filter :confirmation_notice

def confirmation_notice
  flash[:notice] = "my message here" unless current_user.confirmed?
end

Do not forget to configure your views to show this flash notice.

This to learn how to configure your views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM