0
votes

I am trying to implement "Forgot Password" functionality using "devise_token_auth" in Rails 5 api, in which I have carried out the following steps:

  1. Hit the "localhost:3000/auth/password" with registered users email, redirect_url as parameters. I got the email as well.

  2. When clicking the "change my password" link in the mail, it redirects to "localhost:3000/api/auth/password/edit/..." instead of "localhost:3000/auth/password/edit/..."

My reset_password_instructions.html.erb is as follows:

<p><%= t(:hello).capitalize %> <%= @resource.email %>!</p>

<p><%= t '.request_reset_link_msg' %></p>

<p><%= link_to t('.password_change_link'), edit_password_url(@resource, reset_password_token: @token, config: message['client-config'].to_s, redirect_url: message['redirect-url'].to_s).html_safe %></p>

<p><%= t '.ignore_mail_msg' %></p>
<p><%= t '.no_changes_msg' %></p>

I am not able to figure out how "edit_password_url" resolves to "localhost:3000/api/auth/password/edit/..." instead of "localhost:3000/auth/password/edit/...". Could anyone be able to help me here?

Note: In development.rb, I have config.action_controller.default_url_options = { host: 'localhost:3000' }

UPDATE:

Routes.rb

Rails.application.routes.draw do
  #.....
  resources :employee_contact_infos
  resources :employee_work_infos

  mount_devise_token_auth_for 'User', at: 'auth'

  # get '/api' => redirect('/swagger/dist/index.html?url=/apidocs/api-docs.json')
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  get '/industry_sectors' => 'drop_down_values#industry_sector_list'
  get '/currencies' => 'drop_down_values#currency'

  #...

  namespace :employees do

  end


end

rake routes output for devise_token_auth:

                new_user_session GET    /auth/sign_in(.:format)                                                                  devise_token_auth/sessions#new
                    user_session POST   /auth/sign_in(.:format)                                                                  devise_token_auth/sessions#create
            destroy_user_session DELETE /auth/sign_out(.:format)                                                                 devise_token_auth/sessions#destroy
               new_user_password GET    /auth/password/new(.:format)                                                             devise_token_auth/passwords#new
              edit_user_password GET    /auth/password/edit(.:format)                                                            devise_token_auth/passwords#edit
                   user_password PATCH  /auth/password(.:format)                                                                 devise_token_auth/passwords#update
                                 PUT    /auth/password(.:format)                                                                 devise_token_auth/passwords#update
                                 POST   /auth/password(.:format)                                                                 devise_token_auth/passwords#create
        cancel_user_registration GET    /auth/cancel(.:format)                                                                   devise_token_auth/registrations#cancel
           new_user_registration GET    /auth/sign_up(.:format)                                                                  devise_token_auth/registrations#new
          edit_user_registration GET    /auth/edit(.:format)                                                                     devise_token_auth/registrations#edit
               user_registration PATCH  /auth(.:format)                                                                          devise_token_auth/registrations#update
                                 PUT    /auth(.:format)                                                                          devise_token_auth/registrations#update
                                 DELETE /auth(.:format)                                                                          devise_token_auth/registrations#destroy
                                 POST   /auth(.:format)                                                                          devise_token_auth/registrations#create
             auth_validate_token GET    /auth/validate_token(.:format)                                                           devise_token_auth/token_validations#validate_token

Swagger_docs.rb

class Swagger::Docs::Config
  def self.base_api_controller; ApplicationController end
  def self.transform_path(path, api_version)
    # Make a distinction between the APIs and API documentation paths.
    "#{path}"
  end

  Swagger::Docs::Config.register_apis({
                                          '1.0' => {
                                              controller_base_path: '',
                                              api_file_path: 'public/apidocs',
                                              base_path: 'http://localhost:3000',
                                              clean_directory: true
                                          }


                        })

end

1
show your routes.rb and the output of rake routes - arieljuod
have added routes.rb and rake routes output now. - sabhari karthik
In your routes I see edit_user_password_url, not edit_password_url. And I see no reference to /api anywhere neither on the routes.rb or the rake routes output. What do you get if you run rake routes | grep 'api'? - arieljuod
rake routes | grep 'api' is not giving me anything, and if I use edit_user_password_url, it is resolving as "localhost:3000/api/auth/password/edit/2?..." instead of "localhost:3000/auth/password/edit?..."..But, edit_password_url is giving "localhost:3000/api/auth/password/edit?...". So, I am trying to remove that /api part.. - sabhari karthik

1 Answers

1
votes

The generated link gets changed if any configuration for relative url root has been set. Can you check if you have defined in any configuration to set the relative url root as below:

config.relative_url_root = "/api"

Let's assume you want to deploy your application to "/app1". Rails needs to know this directory to generate the appropriate routes.

config.relative_url_root = "/app1"

alternatively you can set the RAILS_RELATIVE_URL_ROOT environment variable.

Rails will now prepend "/app1" when generating links

Check - https://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root