2019-04-05 20:52:04 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-06 19:45:07 +00:00
|
|
|
# El controlador desde el que descienden todos los controladores
|
2019-04-05 20:20:20 +00:00
|
|
|
class ApplicationController < ActionController::API
|
2019-04-06 19:45:07 +00:00
|
|
|
# authenticate! obtiene la pirata a partir de los datos de
|
|
|
|
# autenticacion y la pone en este atributo
|
|
|
|
attr_reader :current_pirata
|
|
|
|
|
|
|
|
# Vamos a usar HTTP Basic Auth
|
|
|
|
include ActionController::HttpAuthentication::Basic::ControllerMethods
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Autenticar a la pirata usando HTTP Basic Auth
|
|
|
|
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
|
|
|
|
def authenticate!
|
|
|
|
@current_pirata ||= authenticate_with_http_basic do |email, password|
|
|
|
|
pirata = Pirata.find_by_email(email)
|
|
|
|
|
|
|
|
if pirata&.authenticate(password)
|
|
|
|
session[:pirata_id] = pirata.id
|
|
|
|
pirata
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Si no la encuentra, no nos deja hacer nada
|
|
|
|
render(json: {}, status: :forbidden) unless current_pirata
|
|
|
|
end
|
2019-04-05 20:20:20 +00:00
|
|
|
end
|