module Api class BaseController < ActionController::API wrap_parameters false before_action :authenticate_api_token! attr_reader :current_user, :current_api_token private def authenticate_api_token! token = ApiToken.authenticate(bearer_token) unless token render json: { error: "Non autenticato" }, status: :unauthorized return end @current_api_token = token @current_user = token.user Current.user = @current_user token.touch_last_used! end def bearer_token header = request.authorization.to_s.presence || request.headers["Authorization"].to_s header[/Bearer\s+(.+)/i, 1] end end end