0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mount_devise_token_auth_forとは

Last updated at Posted at 2025-09-25

記事を作成した目的

備忘録として記録

分からなかった箇所

/config/routes.rb

  namespace :api do
    namespace :v1 do
      mount_devise_token_auth_for "User", at: "auth", controllers: {
        registrations: "api/v1/auth/registrations",
      }
    end
  end

1, mount_とは「RailsのエンジンやRackアプリケーションを既存のRailsアプリケーションに組み込む機能」である。devise_token_authの提供しているルーティングを使うために必要な呪文のようなものとざっくり認識する

自動生成される基本ルート

# POST   /auth              # 新規登録
# POST   /auth/sign_in      # ログイン  
# DELETE /auth/sign_out     # ログアウト
# GET    /auth/validate_token # トークン検証
# PATCH  /auth              # ユーザー情報更新
# DELETE /auth              # アカウント削除

2,at: "auth"はURL「/api/v1」の次に「/auth」のような深いパスを生成する

3,controllers: { registrations: "api/v1/auth/registrations" }は、既存のデフォルトコントローラーをカスタムコントローラーで置き換えている
たとえばデフォルトのままだと、POST /api/v1/auth をリクエストした時DeviseTokenAuth::RegistrationsController#createを呼び出そうとしてしまう。しかし、controllersオプションにてカスタムコントローラーによる置き換えを行なった場合、POST /api/v1/authリクエストはApi::V1::Auth::RegistrationsController#createを呼び出す。これによって正しいルーティング設定ができる。

参考

Claudeによる回答

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?