before_filter with devise

前端 未结 2 1550
无人及你
无人及你 2021-01-02 03:01

I\'m using Devise\'s built in before_filter :authenticate_user!. I want to call my own custom method in my application helper if a user fails the before filter

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-02 03:34

    I would write a custom before filter that uses user_signed_in?. This will just return a boolean, and not do any of the redirect-type actions that authenticate_user! does.

    So, you could write a before filter like this:

    before_filter :custom_user_auth
    ...
    def custom_user_auth
        unless user_signed_in?
            # Do custom stuff, ultimately restricting access to the 
            # ...protected resource if it needs to be
        end
    end
    

    Do note that this before filter won't protect your resource from unauthorized users unless the inside area of that unless statement redirects or renders.

提交回复
热议问题