0

我想爲一個項目製作一個API。我跟着從這個視頻指令 - https://www.youtube.com/watch?v=lgdUqtw4weg&t=165s如何正確設置令牌認證在Rails的?

基本上在視頻中,我爲用戶模型令牌列。並設置控制器,以便我可以使用post方法。 但是,當我運行POST。 我得到的錯誤說

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

我認爲設計與POST干擾,並認爲該用戶沒有試圖訪問非公開頁面時登錄。

我該如何克服這一點並獲得令牌?

這是我的api控制器。

class Api::V1::UserSerializedController < ApplicationController 
protect_from_forgery except: :index 
respond_to :json 


def create 


    user = User.where(email: params[:email]).first 

    if user.valid_password?(params[:encrypted_password]) 
     render json: user.as_json(only: [:email, :authentication_token]),status: :created 

    else 
     head(:unauthorized) 
    end 

end 

def show 

end 
end 

回答

0

你繼承ApplicationController,我猜你authenticate_user!設置有動作之前都有。

你不得不要麼改變父類的東西像ApiController(我喜歡這個)

OR

跳過這個動作你Api::V1::UserSerializedController