0

I am using Rails (3.2.6) with devise (2.1.2) and have a controller where I would like to make sure users are authenticated before proceeding.

Optimistically, I tried...

module Spree
class MyAppController < Spree::BaseController

before_filter :authenticate_user!

...remainder of MyAppController code...

and I am NOT redirected to a login or sign-in page. I am redirected to the 'products' page, with a polite message at the top saying I need to sign-in or sign-up to continue.

What I would like to happen is that I am redirected to sign-up / sign-in and when that is completed successfully, the original controller path resumes.

Searching around, I have read that authenticate_user! from Devise interacts with Spree in such a way as to cause an infinite redirection, so something in Spree disables this, resulting in the lame behavior I describe above.

Has anyone managed to get this to work or have suggestions for a good work-around?

1

2 Answers 2

1

I have found a work around, but I am not an experienced enough Rails developer to know how reasonable this solution is.

I wrote a method to do the filtering and used it instead of authenticate_user!...

def require_authentication
  unless current_user 
    # setting this in the session allows devise to return us to
    # the original invocation path, once sign up / sign in is complete
    session[:user_return_to] = request.env['PATH_INFO']
    redirect_to new_user_session_url and return 
  end 
end
1

did you try adding

before_filter :check_authorization

to your controller?

I think that may do what your looking for.

Thanks

Ash

1
  • Thanks Ash, that should work - but behaves in the same way as above (navigates to the Spree home page (products) and gives me the message about needing to sign in or sign up before proceeding). Obviously, there is something nefarious going on in my code base. It is a project that I inherited and I am not an experienced RoR developer, so I need to isolate the behavior with a small test case. Unfortunately, I have a looming deadline and a work-around, so I'm not sure when I will be able to do that. Thanks again for you help. Jul 3, 2012 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.