0
나는 Ruby On Rails에서 토큰 스토어를 데이터베이스에 저장해야합니까?
나는 토큰이 존재 여부를 확인하는 모델 인증을 생성, 사용자 로그인 시스템을 구축 보석jwt
,
devise
를 사용합니다. 내가
user/sessions
에 POST 요청을 수행 할 때
class Authentication < ApplicationRecord
def self.generate_access_token(email)
payload = {:email => email}
secret = 'secret'
token = JWT.encode payload, secret, 'HS256'
return token
end
end
컨트롤러/사용자/sessions_controller.rb가
def create
user = User.where(email: params[:email]).first
if user&.valid_password?(params[:password])
@token = Authentication.generate_access_token(user.email)
Authentication.create(access_token: @token)
authentications = {token: @token, email: user.email}
render json: authentications, status: :created
else
head(:unauthorized)
end
end
내가 토큰을 얻을 것이다
모델/authentication.rb :
이 코드를 따라 사용자 이메일을 보내어 클라이언트의 로컬 저장소에 저장하고 토큰이 유효한지 확인하는 데 도움을줍니다. 토큰 데이터베이스에 저장할 필요가 없습니다 수 있도록하는 방법이 있습니다, 내 질문에def authenticate_token
token = Authentication.find_by_access_token(params[:token])
head :unauthorized unless token
end
:
이 규정을 따르십시오?
감사합니다. –