Techioz Blog

デバイスではヘルパー メソッドを使用できません

概要

nextjsとrailsを使用してシンプルなtodoアプリを構築しています。 ユーザー認証にdeviceを使用していますが、current_userが使用できません。 具体的には、JSON API Serializerを使用して、ログイン時のユーザー情報をJSON形式で前面に返しています。こちらがレールの配線です。

routes.rb

Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root to: 'health_check#index'

  devise_for :users, skip: %w[registrations sessions]
  devise_scope :user do
    namespace :api do
      namespace :v1 do
        resource :current_user, controller: 'devise/current_user', only: %i[show]
        resource :user_sessions, controller: 'devise/user_sessions', only: %i[create destroy]
        resources :users, controller: 'devise/users' , only: %i[create]
      end
    end
  end
end

以下は api/v1/devise/current_user_controller.rb です

# frozen_string_literal: true

module Api
  module V1
    module Devise
      class CurrentUserController < ApplicationController
        module Consts
          RESP_FIELDS = %i[id email name].map(&:freeze).freeze

          Consts.freeze
        end

        def show
          with_rescue(__method__) do
            render json: current_user_serializable_hash.to_json, status: :ok
          end
        end

        private

        def current_user_serializable_hash
          UserSerializer.new(current_user, { fields: { user: Consts::RESP_FIELDS } }).serializable_hash
        end
      end
    end
  end
end

コントローラー/application_controller.rb

# frozen_string_literal: true

class ApplicationController < ActionController::API
        include ActionController::Cookies
        include WithRescueConcern
end

ここにapp/serializers/user_serializer.rbがあります

# == Schema Information
#
# Table name: users
#
#  id                     :bigint           not null, primary key
#  email                  :string(255)      default(""), not null
#  name                   :string(255)      not null
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#
# Indexes
#
#  index_users_on_email                 (email) UNIQUE
#  index_users_on_name                  (name)
#
class UserSerializer < BaseSerializer
  attributes :id, :email, :name
end

ベースシリアライザー.rb

# frozen_string_literal: true

class BaseSerializer
  include JSONAPI::Serializer

  set_key_transform :camel_lower
end

エラーは次のようになります 先頭に返されるJSONの値はnullです。

{"data":null}

ただし、問題を切り分けるために、current_user_controller の current_user_serializable_hash メソッドをこれに変更しました。

def current_user_serializable_hash
  UserSerializer.new(current_user, { fields: { user: Consts::RESP_FIELDS } }).serializable_hash
end
↓
def current_user_serializable_hash
  UserSerializer.new(User.find(1), { fields: { user: Consts::RESP_FIELDS } }).serializable_hash
end

次に示すように、応答が JSON 形式で返されました。

{
    "data": {
        "id": "1",
        "type": "user",
        "attributes": {
            "id": 1,
            "email": "[email protected]",
            "name": "hoge1"
        }
    }
}

言い換えれば、current_user メソッドが機能していないだけです。なぜだろう。わからない。何が起こっているのか知っている人はいますか?ありがとう。

追伸———————————-

ログイン用の user_sessions_controller コードの一部を本文に追加しました。

# frozen_string_literal: true
module Api
  module V1
    module Devise
      class UserSessionsController < ApplicationController

        def create
          with_rescue(__method__) do
            user = User.find_for_authentication(email: user_session_params[:email])
            raise ApplicationController::UnauthorizedError, 'email' if user.blank?

            is_success = user&.valid_password?(user_session_params[:password])
            raise ApplicationController::UnauthorizedError, 'password' unless is_success

            bypass_sign_in(user)
            head :no_content
          end
        end

解決策

私は自分で問題を解決しました。 具体的には、config/environments/development.rbに以下を追加しました。

config.cache_store = :redis_cache_store, { expires_in: 7.days, # TODO: 仮設定
                                               namespace: "#{Rails.application.class.module_parent_name.downcase}:#{
                                                   (ENV.fetch('RAILS_ENV', 'development') + ':').then.detect { |e| e != 'production:' }
                                                 }cache",
                                               url: "redis://#{ENV.fetch('REDIS_HOST', 'localhost')}:#{ENV.fetch('REDIS_PORT', '6379')}/0" }

    config.public_file_server.headers = {
      'Cache-Control' => "public, max-age=#{2.days.to_i}"
    }

ありがとう。