0

프로젝트 용 API를 만들려고합니다. 이 비디오의 지침을 따랐습니다. https://www.youtube.com/watch?v=lgdUqtw4weg&t=165sRails에서 토큰 인증을 올바르게 설정하는 방법은 무엇입니까?

기본적으로 비디오에서는 사용자 모델에 대한 토큰 열을 만듭니다. 그리고 post 메서드를 사용할 수 있도록 컨트롤러를 설정하십시오. 하지만 POST를 실행할 때. 은 내가 고안이 POST를 방해하고 사용자가 아닌 공개 페이지를 방문하려고 할 때 로그인되지 않았 음을보고 생각

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

말하는 오류가 발생합니다.

토큰을 얻고 토큰을 얻으려면 어떻게해야합니까?

다음은 내 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!이 설정 작업을하기 전에이 같은데요.

당신은

또는

Api::V1::UserSerializedController

위해이 작업을 생략 (내가이 선호하는 것) ApiController 같은 뭔가를 부모 클래스를 변경하려면, 다음 중 하나를 가질 것