Rails: Calling Devise authenticate_user! and handling invalid user/password exception

浪尽此生 提交于 2019-12-23 09:47:49

问题


I have a popup that will only allow to view/save some information if the user is authenticated.

I am using devise.

In the controller before_filter it checks if user is signed in and if not, show a sign in page.

This sign in page is ripped down version of the site's sign in page, so that it fits nicely to the popup.

On the authenticate action I call authenticate_user!. Everything works fine when the user enters valid credentials. But when the credential is invalid, devise automatically redirects to site's sign in page (which as I stated is different and not fit for a popup)

I tried appending a rescue to the call, but to no avail.

Anyone could suggest a better/right way to do this please? :)

def authenticate

    authenticate_user! rescue redirect_to "/popup/sign_in"

    if user_signed_in?
      respond_to do |format|
        format.html {
          flash[:notice] = I18n.t("logged_in_succesfully")
          redirect_back_or_default(accounts_path)
        }

    else
      flash[:error] = I18n.t("devise.failure.invalid")
      render "/popup/sign_in"
    end
  end

来源:https://stackoverflow.com/questions/7501250/rails-calling-devise-authenticate-user-and-handling-invalid-user-password-exce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!