1

ゼロからの簡単な認証があり、ログイン/ログアウト/サインアップ用の RESTfull API が必要です。

ユーザー サインアップは期待どおりに機能します

curl -v -H 'Content-Type: application/json' -H 'Accept: application/vnd.greenapp.v1' -X POST http://localhost:3000/api/users/ -d "{\"user\":{\"email\":\"user1@example.com\",\"password\":\"secret\",\"password_confirmation\":\"secret\"}}"

ただし、sessions#new と sessions#destroy を機能させることができませんでした。これが私のコントローラーです:

module Api
  module V1
    class SessionsController < ApplicationController
      skip_before_filter :verify_authenticity_token,
        :if => Proc.new { |c| c.request.format == 'application/json' }
        respond_to :json

      def create
        user = User.find_by_email(params[:email])
        if user && user.authenticate(params[:password])
          render status: :ok,
                 json: { success: true,
                         info: "Logged in sucessfully.",
                         data: { auth_token: user.auth_token } }
        else
          render status: :unprocessable_entity,
                 json: { success: false,
                         info: "Login failed." }
        end
      end

      def destroy
        cookies.delete(:auth_token)
        render  status: 200,
                json: { success: true,
                        info: "Logged out sucessfully." }
      end

      end
   end
end

ログイン用コマンド

 curl -v -H 'Content-Type: application/json' -H 'Accept: application/vnd.greenapp.v1' -X POST http://localhost:3000/api/sessions -d "{\"user\":{\"email\":\"user1@example.com\",\"password\":\"secret\"}}"

ログ:

Processing by Api::V1::SessionsController#create as JSON
  Parameters: {"user"=>{"email"=>"user1@example.com", "password"=>"[FILTERED]"}, "session"=>{"user"=>{"email"=>"user1@example.com", "password"=>"[FILTERED]"}}}
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."email" IS NULL LIMIT 1
Completed 422 Unprocessable Entity in 1ms (Views: 0.2ms | ActiveRecord: 0.4ms)

ログアウトを処理する方法はありますか?auth_token に基づいている必要がありますか?

4

1 に答える 1