1

I'm trying to implement a 'Remember Me' feature in the new Padrino 0.11 Admin interface, but having a little bit of trouble due to the differences between it and Rails. Basically, I'm following along with http://railscasts.com/episodes/274-remember-me-reset-password.

I've managed to get the Remember Me and auth_token working handily, and I can see the cookie in the Dev console when I go to look at it. I am having a lot of trouble figuring out how to get the application to do autologin on the cookie when it is present though. I'm sure it's something stupid, but this is where I'm up to.

For instance, I've got the actual Remember Me creating an auth_token and setting it fine to the cookie (I can see it on localhost) in the dev console on Chrome via this in the sessions controller.

admin/controllers/sessions

post :create do
  if account = Account.authenticate(params[:email], params[:password])
    set_current_account(account)
    if params[:remember_me]
      response.set_cookie('da_app', value: account.auth_token,
                              expires: (Time.now + 1.year + 1.day))
    end
    flash[:success] = "You've successfully logged in as #{account.name}."
    redirect url(:base, :index)
  else
    params[:email], params[:password] = h(params[:email]), h(params[:password])
    flash[:error] = pat('login.error')
    redirect url(:sessions, :new)
  end
end

However, due to my inexperience with padrino, a little stumped as to where I'd put the bit of logic which triggers before an incoming request, checks for the cookie and then logs the user in. I tried the following, which is not perfect but which is definitely not working (though not sure why... =< ) and in fact, the code block to detect the cookie does not even seem to be firing (which seems pretty basic.).

admin/app.rb (not sure this is the right place for it actually)

before '/*' do
  if request.cookies['da_app'].exists?
    set_current_account(Account.find_by_auth_token(request.cookies['da_app']))
    redirect url(:base, :index)
  end
end

So, I'm sure it's probably dead simple to solve but a bit stumped on this one (and also, am really trying to avoid using a gem plugin like padrino-warden or the like at the moment and implement this from scratch as an exercise.).

(Also, bonus karma points on helping solve this one as I'm implementing this as part of some pro bono work for a global conservation charity.)

Daryl
  • 23
  • 4

0 Answers0