Why I'm getting undefined method `cookies'?
Asked Answered
U

2

6

I'm trying to setup Authlogic with single_access_token for my rails backend app (I'm using rails-api gem).

I'm following this example: http://blog.centresource.com/2013/06/04/using-ember-auth-with-rails-3-and-authlogic/

So far, I'm able to create users, but when I try to login it fails on user_session.save:

NoMethodError in Api::V1::UserSessionsController#create
undefined method `cookies' for #<Api::V1::UserSessionsController:0x00000004528cb8>

According to similar questions I've found here, I have to configure Authlogic as:

  acts_as_authentic do |c|
    c.login_field = :email
    c.maintain_sessions = false
  end

on my User model. However, it keeps failing. I'm assuming when UserSession is saved, Authlogic tries to update the magic fields on User, but I have no idea why is still trying to use cookies.

Ugh answered 27/4, 2015 at 18:48 Comment(0)
M
22

The tutorial assumes a regular rails app, but you're using rails-api. By default controllers in rails-api don't include the middleware that handles cookies. If you need cookies then you need to add that middleware:

config.middleware.use ActionDispatch::Cookies

And then include ActionController::Cookies in your controller. There's more info on what's included by default in the rails-api README

Macadamia answered 27/4, 2015 at 19:8 Comment(2)
I know, but I would like to avoid using Cookies for the application since it doesn't make sense for an API. According to the documentation of Authlogic, it should be possible to make it work without cookies.Ugh
Including config.middleware.use ActionDispatch::Cookies in the controller solved it for me.Empiricism
S
16

Rails-api does have additional steps to accept cookies

  1. add config.middleware.use ActionDispatch::Cookies to config/application.rb
    1. add at the top under class include ActionController::Cookies in your expected controller

Restart the application

Septic answered 16/11, 2019 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.