0

I've got a Rails 3.2.8 app using Sorcery for authentication. Sorcery provides a current_user method, pretty standard stuff.

My app has subscriptions, they work pretty much in the standard resourceful way. Here's the abridged version of the controller:

class SubscriptionsController < ApplicationController
  before_filter :require_login
  force_ssl

  def show
    @subscription = SubscriptionPresenter.new( current_user )
  end

  def create
    handler = StripeHandler.new( current_user )
    ...
  end

  def destroy
    handler = StripeHandler.new( current_user )
    ...
  end
end

The #show action works fine, current_user loads. However, right now #create does not work, because current_user ends up being nil in that action.

So, why is current_user nil when a logged in user posts to this action? My guess is something about the way sessions work over SSL, but I don't know what I'm missing here...

6
  • I don't know Sorcery but I'm assuming there's something like an authentication token or session token or so? If so, does it get sent with every request? If so, does it change or not?
    – Vapire
    Oct 16, 2012 at 17:02
  • I believe Sorcery uses a session token, to be honest I'm not sure exactly how it works.
    – Andrew
    Oct 16, 2012 at 17:04
  • Look in the rendered HTML code in your browser if you can find a session token or so before your form gets submitted. If there's nothing there I'd say it's a good chance that this is the problem...
    – Vapire
    Oct 16, 2012 at 17:25
  • Is your csrf_token being sent?
    – axsuul
    Oct 16, 2012 at 20:07
  • @Axsuul, looks like it is not being sent. Why isn't it being generated with form_tag ... I thought that was automatic? Is there any way to fix that besides manually using form_tag nil in the form?
    – Andrew
    Nov 1, 2012 at 23:18

1 Answer 1

0

I figured this out. It turns out that I was actually getting a silent exception in a 3rd-party library that I was interacting with, and that exception was causing an 'unauthorized' request which logged the user out. After patching that it turns out there was nothing wrong with my controller specifically. Thanks for the pointers, all.

2
  • 2
    can you give us a little more details on how you patched this up? I'm having the same problem.
    – styliii
    Dec 15, 2013 at 20:45
  • It's been over a year, and to be honest I don't recall the details of the situation. Basically I was having an exception somewhere in the handler object, and there was a rescue that just returned nil instead, so I was getting nil and no error message. I would guess that I went through the control flow line by line until I found the rescue that was catching something it shouldn't have, or I changed the behavior to not rescue any more, or something like that. Sorry I can't be more specific. Good luck!
    – Andrew
    Dec 16, 2013 at 0:42

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.