実現したいこと
Railsのgemであるdevise、devise_token_authを利用してcurrent_userヘルパーメソッドで、ログインユーザーを取得したい。
前提
devise_token_authを利用して
新規登録、
ログイン、
ログアウト、
は実装済みなのですが、current_userがnilになる事象が起こっています。
以下はルーティングとコントローラーの設定です。
ルーティング
Rails.application.routes.draw do namespace :api do namespace :v1 do mount_devise_token_auth_for 'User', at: 'auth', controllers: { registrations: 'api/v1/auth/registrations' } namespace :auth do resources :sessions, only: %i[index] end end end end
このようなルーティングになっているのでcurrent_userは「current_api_v1_user」に変えています。
コントローラー
class Api::V1::Auth::SessionsController < ApplicationController def index if current_api_v1_user render json: { is_login: true, data: current_api_v1_user } else render json: { is_login: false, message: "ユーザーが存在しません" } end end end
この結果、返されるのはelseの方でなぜだか分からず詰まっています。
補足情報(FW/ツールのバージョンなど)
Ruby 3.2.2
Rails 7.0.8
情報が足りないなどのお声がありましたら、教えてもらえると助かります。
ご回答よろしくお願いいたします。
あなたの回答
tips
プレビュー