簡體   English   中英

如何在Rails中制作授權JSON API?

[英]How To Make Authorization JSON API in Rails?

大家好,我一直在考慮JSON API授權。 我已經成功創建了一個用於評論的API,但是當我(使用令牌)調用評論端點時,它會顯示數據庫中的所有評論, 我想做的就是顯示與當前用戶關聯的評論。 我該如何實現? 先感謝您。

這是我的評論控制器代碼:

module Api
    module V1
        class ReviewsController < Api::ApiController
            respond_to :json
            before_action :authenticate 

            def index
                respond_with Review.all
            end

            def show
                respond_with Review.find(params[:id])
            end

            def create
                respond_with Review.create(params[:product])
            end

            def update
                respond_with Review.update(params[:id], params[:products])
            end

            def destroy
                respond_with Review.destroy(params[:id])
            end

            private

              def authenticate
              authenticate_or_request_with_http_token do |token, options|
              User.find_by(access_token: token)
              end

           end

        end
    end
end

代替

User.find_by(access_token: token)

@user = User.find_by(access_token: token)

然后在索引操作中...

respond_with @user.reviews

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM