2012-03-09 16 views
5

を考案私はセットアップには、この優れた記事私のレールの真偽一部を以下のよ(3.2)API:トークンのRailsによる認証と

-Added工夫:私は、次のステップを行っている
http://blog.joshsoftware.com/2011/12/23/designing-rails-api-using-rabl-and-devise/

Gemfileする

は、ユーザモデルのために考案-Enabledと

- 私のユーザモデルが

あるのに必要な移行を走りました ​​

と同様に、データベースのtoken_authenticable(移行による)。

がでRegistrationControllerを-Subclassed:

class RegistrationsController < Devise::RegistrationsController 
    def new 
    super 
    end 

    def create 
    resource = warden.authenticate!(:scope => resource_name, :recall => " {controller_path}#new") 
    sign_in(resource_name, resource) 
    current_user.reset_authentication_token! 
    respond_with resource, :location => after_sign_in_path_for(resource) 
    end 

    def update 
    super 
    end 
end 

-In routes.rbをし、私が持っている:

devise_for :users, :controllers => {:registrations => "registrations"} 

ユーザー作成

私が作成するには、次のリクエストを希望しますユーザーを認証して返信する:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"user":{"email":"[email protected]", "password":"pass"}}' 'http://localhost:3000/users.json 

私の理解では、ロジックは登録コントローラの「作成」メソッド(ユーザーを作成して同時にログインさせる必要があります)に入れなければなりません。

{"error":"You need to sign in or sign up before continuing."} 

作成し、ログインして新しいユーザーを持っている行方不明の作品がどのようなものです:私は、私はお返しになったメッセージがあるように私が間違っているべきだと思いますか? RegistrationController#createにマップされたusers.jsonへのPOSTは行いませんか?

は、ユーザーのログインも

、私は私が推測する

curl -H "Accept: application/json" -H "Content-type: application/json" -X GET -d '{"user":{"email":"[email protected]","password":"pass"}}' 'http://localhost:3000/users.json 

(ログイン/パスワードが確認された後、彼authentification_token彼を送り返す)でユーザーをログインし、次のリクエストを希望しますロジックはRegistrationControllerの "update"メソッドに入るはずですが、100%確実ではありません。ログインが完了したら、他のモデルの作成/表示を保護するためにトークンの認証を追加します。ユーザーがいないauthentication_tokenで、なぜ作成して署名されていない理由を任意のアイデア

Started POST "/users.json" for 127.0.0.1 at 2012-03-11 20:50:05 +0100 
Processing by RegistrationsController#create as JSON 
Parameters: {"user"=>{"email"=>"[email protected]", , "password"=>"[FILTERED]", "phone"=>"1234567890"}, "registration"=>{"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "phone"=>"1234567890"}, "action"=>"create", "controller"=>"registrations", "format"=>"json"}} 
WARNING: Can't verify CSRF token authenticity 
Completed 401 Unauthorized in 1ms 

UPDATE

私が発行します。

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"user":{"email":"[email protected]", "password": "mypass", "phone":"1234567890"}}' 'http://localhost:3000/users.json' 

私は、次のメッセージが表示されました返されますか?

+0

認証前にユ​​ーザーを作成する場合は、そのコードを追加する必要があります。 Deviseはそれをしません。 –

答えて

10

私のせいで、私はブログ記事を更新します。 あなたは

if params[:api_key].blank? or params[:api_key] != API_KEY 
    render :json => {'errors'=>{'api_key' => 'Invalid'}}.to_json, :status => 401 
    return 
end 
build_resource 
if resource.save 
    sign_in(resource) 
    resource.reset_authentication_token! 
    #rabl template with authentication token 
    render :template => '/devise/registrations/signed_up' 
else 
    render :template => '/devise/registrations/new' #rabl template with errors 
end 

はあなたがどんな問題に直面した場合、私に教えてくださいあなたの登録コントローラにユーザーを作成するために、次のコードを追加する必要がありますか?

+0

ありがとう、登録は正常に動作しています。 sign_inの部分に関しては、変更するコードはSessionsControllerの「作成」メソッドにあると認識していますか?この場合、ログイン用のパスワードは認証用にクリアに送信されていますか? – Luc

+0

sessions_controller#次の行を 'sign_in(resource_name、resource)の後ろに追加する必要があります current_user.reset_authentication_token' – Sethupathi

0

リュックの質問については、これも私の理解です。

たとえば、デフォルトの非APIログインを使用すると、SessionsController.createがログインを処理します。 authentication_tokenを取得してAPIコールの一部として後で再利用するにはどうすればよいですか? SessionsController.createを無効にしてresource.reset_authentication_token!に電話するにはどうすればよいですか?

+0

非apiを使用している場合は、current_user.authentication_tokenとしてアクセスできます – Sethupathi

関連する問題