Rails 3 disabling session cookies

前端 未结 10 1950
梦如初夏
梦如初夏 2020-11-30 00:06

I have RESTful API written on RoR 3. I have to make my application not to send \"Set-Cookie header\" (clients are authorizing using auth_token parameter).

I have tri

10条回答
  •  無奈伤痛
    2020-11-30 00:43

    I'm not sure when they added it to Devise, but there appears to be a configuration that will let you disable the sending of the session cookie when using a auth_token:

    # By default Devise will store the user in session. You can skip storage for
    # :http_auth and :token_auth by adding those symbols to the array below.
    # Notice that if you are skipping storage for all authentication paths, you
    # may want to disable generating routes to Devise's sessions controller by
    # passing :skip => :sessions to `devise_for` in your config/routes.rb
    config.skip_session_storage = [:http_auth, :token_auth]
    

    It does work well. The only issue I had was that I still needed to be able to make an initial request to my token_controller in order to generate/retrieve the token. I.e. POST /api/v1/tokens.json, which unfortunately would cause a session cookie to be returned for that request.

    So I ended up implementing the CookieFilter intializer that Ryan Ahearn wrote above anyway.

    Also, since my app has both a web front-end as well as a JSON api, I only wanted to filter the cookies for the JSON api. So I modified the CookieFilter class to first check the requests belonged to the api:

    if env['PATH_INFO'].match(/^\/api/)
      Rack::Utils.delete_cookie_header!(headers, '_myapp_session')
    end
    

    Not sure if there's a better way of doing that...

提交回复
热议问题