lynndylanhurley / lynndylanhurley/devise_token_auth
Documentation does not reflect that autogenerated method names are influenced by mountpoint namespacing
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 3.6k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
Today I have spent a lot of hours debugging why in my controllers the authenticate_user! current_user and similar methods did not show up.
In the documentation it states
Note that if the model that you're trying to access isn't called User, the helper method names will change. For example, if the user model is called Admin, the methods would look like this:
- before_action :authenticate_admin!
- admin_signed_in?
- current_admin
However, I finally figured out that the controller did have methods like authenticate_api_v1_user! and current_api_v1_user.
Turns out that at what location in your routes you specify the mountpoint matters significantly: I had namespaced the mount_devise_token_auth_for under api/v1 as this seemed the proper way to ensure fowards compatibility with the authentication part of my API:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
namespace :api, defaults: {format: :json} do
namespace :v1 do
mount_devise_token_auth_for 'Merchant', at: 'auth/merchant'
mount_devise_token_auth_for 'User', at: 'auth'
# ... Actual paths of the API omitted.
end
end
end
So this results in authenticate_api_v1_user!, current_api_v1_user and api_v1_user_signed_in?. (as well as authenticate_api_v1_merchant!, current_api_v1_merchant and api_v1_merchant_signed_in? for the second mountpoint)
I hope the documentation can be updated to clearly indicate that the mount path influences the naming of these helper functions, so that other people do not need to experience this frustration.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the documentation section describing the helper names generated for a non-User model, then compare it with the routes example using mount_devise_token_auth_for under api/v1. Update the documentation to explain that the mount path affects names such as authenticate_api_v1_user! and current_api_v1_user, and include the relevant namespacing example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- authentication, documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100