2012-04-22 7 views
2

私はログインにdeviseを使用するアプリケーションを持っています。私はデータベースにユーザーを認証し、単純にjson文字列として@userハッシュを返すWebアプリケーションのログインフォームを持っています。{render json:}を使用した場合の属性がありません

の目標は、ユーザーがユーザーを認証し、継続的にログインすることからユーザーを防ぐために、アプリで将来の使用のために彼らのauthentication_tokenを取得取得することです。

の問題は、私はauthentication_token得ることができないということです返されたjsonに含める必要があります。

マイuser.rbモデル

attr_accessible :authentication_token, :email, :password, :password_confirmation, :remember_me, :bio, :confirmed, :deposit, :email, :fri25, :mon21, :name, :paid, :picture, :sat26, :sun20, :sun27, :thur24, :tue22, :wed23 

は明らかにauthentication_token記号が含まれています。

セッションコントローラには、単純な認証方法を実行し、jsonとして@userハッシュで応答するnewApiというカスタムアクションがあります。

def newapi 
@user = User.authenticate(params[:email],params[:password]) 
respond_to do |format| 
    format.json { render json: @user } 
end 

EEND

しかし、どんなに私は、認証トークンがJSONレスポンスには含まれていません何をすべきか。私は明白な何かを欠いていますかなぜそれが含まれていないのですか?

答えて

-1

私はjbuilderを使用して問題を自分で解決することになりました。

基本的には、render json: @userコールを削除してjbuilderテンプレートを使用します。それが十分にあるとして、あなたがこのルートを得た場合、そのドキュメントは、私は必要なすべてを行い、より多くの、私たちは今、APIの戻りを行う唯一の方法であるnewapi.json.jbuilder

json.(
    @user, 
    :authentication_token, 
    :email, 
    :password, 
    :password_confirmation, 
    :remember_me, 
    :bio, 
    :confirmed, 
    :deposit, 
    :email, 
    :fri25, 
    :mon21, 
    :name, 
    :paid, 
    :picture, 
    :sat26, 
    :sun20, 
    :sun27, 
    :thur24, 
    :tue22, 
    :wed23 
) 

レビューで

。あなたがそれをチェックアウトする前にそれを使用していない場合は!それはAPIエンドポイントを驚くほど簡単にします。

Nick Knudsonの回答は、独自の設定をロールしたい場合にも有効です。

+0

解決策はどこですか? 解決策を提示せずにあなた自身の質問に「答えました」と答えると、あなたの解決策につながった答えが投票されませんでした。これは本当にStackoverflowを使用する方法ではありません。 – lcguida

+0

例を提供するために答えを更新しました。 – JonathanSimmons

4

Devise> 2.xは、xmlとjsonの応答から属性を除外しているようです。これはおそらくセキュリティ機能ですが、どこに導入されたのか分かりません。 「保護属性」のリストはこちら:

https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb

行54:

BLACKLIST_FOR_SERIALIZATION = [:encrypted_password, :reset_password_token, :reset_password_sent_at, 
    :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, 
    :last_sign_in_ip, :password_salt, :confirmation_token, :confirmed_at, :confirmation_sent_at, 
    :remember_token, :unconfirmed_email, :failed_attempts, :unlock_token, :locked_at, :authentication_token] 

、それはライン122から135

上で初期化するコードのコメントをあなたができると言いますブラックリストに載っている属性のリストを提供するか、それぞれ:force_except:exceptを使用して既存のリストに追加します。

私のハックソリューションは、このようになります:

def newapi 
    @user = User.authenticate(params[:email],params[:password]) 
    respond_to do |format| 
    format.json { render json: @user.as_json(:force_except => Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION.delete_if{|x| x == :authentication_token} } 
    end 
end 

しかし、私はどこかに、よりエレガント初期化子のように、このリストをオーバーライドする方法があるだろうと思っています。私の作品は、最高の答えではない可能性が高いです。

1

私の回避策:

class User < ActiveRecord::Base 
    # include devise token_authenticatable and others 
    devise :token_authenticatable 

    # after devise finds our user (such as when logging in) 
    after_find :write_auth_token 

    # write our pseudo attribute to the model 
    def write_auth_token 
    write_attribute(:auth_token, self.authentication_token) 
    end 

    # make our pseudo attribute attr_accessible 
    attr_accessible :auth_token 
end 
2

これは古いですが、すべての属性が技術的にモデル上でアクセス可能な方法となっているので、私がしたすべては、その「方法」オプション

to_json(methods: :arbitrary_method) 

で使用しました。これは、最も手間のかからないように見えた:このページではD

http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html

0

@Travisトッドの答えは完全に作業を行い、そして、はい、よりエレガントな解決策があるかもしれません。さらに別のを待っている

def newapi 
    @devise_attrs = [:encrypted_password, :remember_created_at, :sign_in_count, :current_sign_in_at] #etc.. 
    @user = User.authenticate(params[:email],params[:password]).includes(:identity) 
    respond_to do |format| 
    # I added a few other options and updated ruby syntax so that you can see how can this be customized if in need of specific user attributes or relations. 
    format.json { render json: @user.as_json(force_except: Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION.delete_if{|x| @devise_attrs.include?(x)}, include: [:identity]), status: 200 } 
    end 
end 

:あなたは、DBの移行や、あなたがこれらのブラックリスト属性の2以上を必要とする他の同様の目的のためにプライベートまたは一時的なAPIを構築する場合はまた、あなたのような何かをしたいかもしれませんこれを指で舐めるエレガントなソリューション。

関連する問題