ActiveAdmin with Devise,登录时不返回authentication_token

时间:2013-04-17 18:23:06

标签: ruby-on-rails devise activeadmin

我有一个使用User模型的ActiveAdmin应用程序(而不是默认的AdminUser)。用户有角色(空白或管理员)

app / model / user.rb:

class User < ActiveRecord::Base
  devise :database_authenticatable, 
     :recoverable, :rememberable, :trackable, :validatable,
     :token_authenticatable

      attr_accessible :email, :password, :password_confirmation, :remember_me, :role

      has_and_belongs_to_many :groups
      has_many :items, :through => :groups

      before_save :ensure_authentication_token

      def is_admin?
        @role.eql?("admin") ? true : false
      end
end

config / routes.rb:

Lao::Application.routes.draw do
  ActiveAdmin.routes(self)
  devise_for :users, ActiveAdmin::Devise.config
end

在DeviseCreateUser迁移中,我添加了:

## Token authenticatable
t.string :authentication_token

如果我登录以下HTTP POST请求:

curl -H 'Content-Type: application/json' -H 'Accept: application/json' -XPOST http://localhost:3000/admin/login.json -d '{"user" : {"email" : "user@email.com", "password" : "password"}}'

我没有获得身份验证令牌:

=> {"created_at":"2013-04-17T17:11:38Z","email":"user@email.com","id":2,"role":null,"updated_at":"2013-04-17T17:53:37Z"}

如何在响应中获取身份验证令牌?我无法找出为什么“created_at”,“email”,“id”,“role”和“updated_at”字段是发送的字段。如何更改此列表?

1 个答案:

答案 0 :(得分:1)

请参阅此answer以获取更深入的内容。您需要使用返回所需json的内容覆盖ActiveAdmin :: Devise :: SessionsController #create。

最简单(而不是理想)的方法就是添加

class ActiveAdmin::Devise::SessionsController
  def create
    #your session creation code
  end
  def destroy
    #your session destruction code
  end                 
end

在config / active_admin.rb的最底部,这将取代AA的会话创建/销毁方法。

有关更简单的替换代码,请参阅this gist和最后一条评论。