开发者

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

开发者 https://www.devze.com 2023-04-06 05:26 出处:网络
I have a popup that will only allow to view/save some information if the user is authenticated. I am using devise.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号