Сообщение об ошибке, которое я получаю, гласит:
Filter chain halted as :authenticate_request rendered or redirected
Completed 401 Unauthorized in 1ms
Я пытаюсь получить доступ к странице с именем localhost:3000/items
Я полностью избавился от before_action
и получилчтобы запустить, но мне нужна аутентификация для работы.
РЕДАКТИРОВАТЬ
Я получаю токен, выполняя в терминале следующее:
$ curl -H "Content-Type: application/json" -X POST -d '{"email":"example@mail.com","password":"123123123"}' http://localhost:3000/authenticate
Когда я получаю токен, я пытаюсь передать ему следующее:
$ curl -H "Authorization: 123EXAMPLETOKEN123" http://localhost:3000/items
Вот мой application_controller, который, я считаю, является основным источником моей проблемы
class ApplicationController < ActionController::API
before_action :authenticate_request
attr_reader :current_user
private
def authenticate_request
@current_user = AuthorizeApiRequest.call(request.headers).result
render json: { error: 'Not Authorized' }, status: 401 unless @current_user
end
end
Следующеекод, который я считаю, также может быть связан с проблемой, мой AuthorizeApiRequest
class AuthorizeApiRequest
prepend SimpleCommand
def initialize(headers = {})
@headers = headers
end
def call
user
end
private
attr_reader :headers
def user
@user ||= User.find(decoded_auth_token[:user_id]) if decoded_auth_token
@user || errors.add(:token, 'Invalid token') && nil
end
def decoded_auth_token
@decoded_auth_token ||= JsonWebToken.decode(http_auth_header)
end
def http_auth_header
if headers['Authorization'].present?
return headers['Authorization'].split(' ').last
else
errors.add(:token, 'Missing token')
end
nil
end
end