I'm building a Rails application with a Javascript framework, so Rails is serving the backend API.
For now, the app is simply implementing all the devise views and actions.
In order to do so, the Rails app accepts only JSON calls to its /api/
URLs, and requires that Devise is working with JSON calls only, so that I defined them like the following:
Rails.application.routes.draw do
scope :api, module: :api, constraints: { format: 'json' } do
devise_for :users, controllers: {
confirmations: 'devise/confirmations',
registrations: 'devise/registrations',
sessions: 'sessions'
}
resources :users
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: 'home#index'
get '*path', to: 'home#index'
end
To explain what is going on here:
- First part is defining the API URLs including the devise ones and defines the API resources (for now only
:users
) - Then it defines the
root
route - Finally forwards any calls to the Javascript router (allowing to manage page reload or external links like the email links).
All is working fine with this, excepted the account confirmation email link.
The confirmation email sent has a link including /api/
(http://localhost:3001/api/users/confirmation?confirmation_token=XLDVqqMZwXg6dszyV_nc) while it should be without (Expected URL: http://localhost:3001/users/confirmation?confirmation_token=XLDVqqMZwXg6dszyV_nc).
How can I make devise sending the confirmation email (and all the other emails) without the /api/
part?
Update
Looking deeper the devise source code I found that the confirmation mailer view template is using the confirmation_url
Rails named route, which is correct.
In my case, I need ALL the devise routes to be limited to the /api/
route, and to the JSON format, excepted few routes to be 2 times defined: a first time out of the /api/
scope in HTML format (which will be forwarded to my JavaScript app), and a second route within the /api/
scope which will be called by the JavaScript app.
Example: Expected account creation confirmation execution stack
- Rails receives the request to
/users/confirmation?confirmation_token=XLDVqqMZwXg6dszyV_nc
- Rails forward the request to the Javascript router
- Javascript framework loads the corresponding page
- Javascript page loads a service which will call the
/api/users/confirmation?confirmation_token=XLDVqqMZwXg6dszyV_nc
Devise route to confirm the account