2013-04-17 2 views
3

기본 AdminUser 중 하나 대신 User 모델을 사용하는 ActiveAdmin 앱이 있습니다.ActiveAdmin with Devise는 로그인시 authentication_token을 반환하지 않습니다.

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 :

DeviseCreateUser 마이그레이션에서
Lao::Application.routes.draw do 
    ActiveAdmin.routes(self) 
    devise_for :users, ActiveAdmin::Devise.config 
end 

, 내가 추가 한 : 사용자는 응용 프로그램/모델/user.rb이

(공백 또는 관리자) 역할을한다

## 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" : "[email protected]", "password" : "password"}}' 

나는 인증 토큰을 얻을하지 않습니다

=> {"created_at":"2013-04-17T17:11:38Z","email":"[email protected]","id":2,"role":null,"updated_at":"2013-04-17T17:53:37Z"} 
내가 응답의 인증 토큰을 얻을 수있는 방법

? 나는 "created_at", "email", "id", "role"및 "updated_at"필드가 발송 된 이유를 알 수 없습니다. 이 목록을 어떻게 바꿀 수 있습니까?

답변

1

이 부분은 상당히 깊이있는 내용입니다.이 answer을 참조하십시오. ActiveAdmin :: Devise :: SessionsController # create를 원하는 json을 반환하는 것으로 재정의해야합니다.

그렇게 할 수있는 가장 쉬운 (그리고 이상적이지) 방법은 단지 설정/active_admin.rb의 맨 아래에

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

을 추가하는 것입니다, 이것은 당신과 AA의 세션 생성/제거 방법을 대체합니다.

간편한 교체 코드는 this gist과 마지막 코멘트를 참조하십시오.

관련 문제