我有一个 ActiveAdmin 应用程序,它使用 User 模型(而不是默认的 AdminUser 模型)。用户有一个角色(空白或管理员)
应用程序/模型/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
配置/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"}}'
我没有得到身份验证 token :
=> {"created_at":"2013-04-17T17:11:38Z","email":"user@email.com","id":2,"role":null,"updated_at":"2013-04-17T17:53:37Z"}
如何在响应中获取身份验证 token ?我不知道为什么“created_at”、“email”、“id”、“role”和“updated_at”字段是发送出去的。如何更改此列表?
最佳答案
看到这个 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 的 session 创建/销毁方法。
有关更简单的替换代码,请参阅 this gist和最后的评论。
关于ruby-on-rails - 带有设计的 ActiveAdmin,登录时不返回 authentication_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16067356/