Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Route): Accept override #1432

Merged
merged 1 commit into from
Nov 1, 2020

Conversation

plribeiro3000
Copy link
Contributor

I have a rails app with devise login and i'm adding a new auth using this gem but i can't have both working at the same time because of the way it handles the call to devise.

@@ -16,6 +16,9 @@ def mount_devise_token_auth_for(resource, opts)
omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || 'devise_token_auth/omniauth_callbacks'
unlocks_ctrl = opts[:controllers][:unlocks] || 'devise_token_auth/unlocks'

# check for resource override
route = opts[:as] || resource.pluralize.underscore.gsub('/', '_')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like a test for this but otherwise can you give me an example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure how to test this in the beginning so i thought to get earlier feedback before dive into the test codebase.

So, we have a rails app with default devise sign in routes and we are migrating the frontend to an angular app which does authenticate using devise_token_auth.

The problem is that i cannot change the current route of authentication we are using with devise and i could not figure out how to tell devise_token_auth to use a different route instead of /users.

Does this makes sense?

Copy link
Collaborator

@MaicolBen MaicolBen Oct 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't at worked? it's on mount_devise_token_auth_for 'User', at: 'REPLACE ME'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, because at just changes the path, it still does work on top of the same resource which i guess devise does something and breaks the other call around.

If i just switch as for at when i try to sign in at devise default route it gives me a devise_token_auth authentication issue.

Copy link
Collaborator

@MaicolBen MaicolBen Oct 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gem works on top of devise so I don't know what's your issue, can you post your routes? The as should change the path name which is a code thing, not an API thing. That or a test case will solve my concern

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. This is my setup using as:

# config/routes.rb
Rails.application.routes.draw do
  devise_for :users,
             controllers: { sessions: 'devise/custom_sessions' },
             skip: :registrations,
             path: ''

  devise_scope :user do
    resource :registration,
             only: %i[edit update],
             path: :profile,
             controller: 'devise/custom_registrations',
             as: :user_registration
  end
  mount_devise_token_auth_for 'User', at: 'auth', as: 'devise_token_auth'
end

And this is what i get from bin/rails routes | grep devise:

                         new_user_session GET      /sign_in(.:format)                           devise/custom_sessions#new
                             user_session POST     /sign_in(.:format)                           devise/custom_sessions#create
                     destroy_user_session DELETE   /sign_out(.:format)                          devise/custom_sessions#destroy
                   edit_user_registration GET      /profile/edit(.:format)                      devise/custom_registrations#edit
                        user_registration PATCH    /profile(.:format)                           devise/custom_registrations#update
                                          PUT      /profile(.:format)                           devise/custom_registrations#update
            new_devise_token_auth_session GET      /auth/sign_in(.:format)                      devise_token_auth/sessions#new
                devise_token_auth_session POST     /auth/sign_in(.:format)                      devise_token_auth/sessions#create
        destroy_devise_token_auth_session DELETE   /auth/sign_out(.:format)                     devise_token_auth/sessions#destroy
    cancel_devise_token_auth_registration GET      /auth/cancel(.:format)                       devise_token_auth/registrations#cancel
       new_devise_token_auth_registration GET      /auth/sign_up(.:format)                      devise_token_auth/registrations#new
      edit_devise_token_auth_registration GET      /auth/edit(.:format)                         devise_token_auth/registrations#edit
           devise_token_auth_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
                             auth_failure GET      /auth/failure(.:format)                      devise_token_auth/omniauth_callbacks#omniauth_failure
                                          GET      /auth/:provider/callback(.:format)           devise_token_auth/omniauth_callbacks#omniauth_success
                                          GET|POST /omniauth/:provider/callback(.:format)       devise_token_auth/omniauth_callbacks#redirect_callbacks
                         omniauth_failure GET|POST /omniauth/failure(.:format)                  devise_token_auth/omniauth_callbacks#omniauth_failure

And if i take the as out i get this from bin/rails routes | grep devise:

                         new_user_session GET      /sign_in(.:format)                       devise/custom_sessions#new
                             user_session POST     /sign_in(.:format)                       devise/custom_sessions#create
                     destroy_user_session DELETE   /sign_out(.:format)                      devise/custom_sessions#destroy
                   edit_user_registration GET      /profile/edit(.:format)                  devise/custom_registrations#edit
                        user_registration PATCH    /profile(.:format)                       devise/custom_registrations#update
                                          PUT      /profile(.:format)                       devise/custom_registrations#update
                         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
                 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
                             auth_failure GET      /auth/failure(.:format)                  devise_token_auth/omniauth_callbacks#omniauth_failure
                                          GET      /auth/:provider/callback(.:format)       devise_token_auth/omniauth_callbacks#omniauth_success
                                          GET|POST /omniauth/:provider/callback(.:format)   devise_token_auth/omniauth_callbacks#redirect_callbacks
                         omniauth_failure GET|POST /omniauth/failure(.:format)              devise_token_auth/omniauth_callbacks#omniauth_failure

As you can see is the same but the behaviour is a bit weird. When accessing localhost:3000 i should get redirect to http://localhost:3000/sign_in to sign in but instead i get redirect to http://localhost:3000/auth/sign_in with a json message:

{
  success: false,
  errors: [
    "Use POST /sign_in para efetuar o login. GET não é suportado."
  ]
}

If i try to access http://localhost:3000/sign_in directly i get a 404.

The only way i got it fixed was with the as option above.

Does this help?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit weird and I think we are missing something in order to spot the issue. But this change doesn't seem to harm a lot

@MaicolBen MaicolBen merged commit 24da31e into lynndylanhurley:master Nov 1, 2020
@plribeiro3000 plribeiro3000 deleted the override-route branch November 3, 2020 11:48
cs0511 added a commit to HeadsUpHealth/devise_token_auth that referenced this pull request Nov 9, 2021
commit c922580
Author: ‮Artem Kozaev <artemkozaev@gmail.com>
Date:   Tue Dec 8 22:58:00 2020 +0300

    Add email validation errors via add (lynndylanhurley#1445)

commit 72ccd90
Author: Hu Hailin <tony.hu.hailin@gmail.com>
Date:   Wed Dec 9 04:57:39 2020 +0900

    rc causes issue for namespaced class, use mapping directly to keep behavior consistency with devise (lynndylanhurley#1440)

commit 8dba30b
Author: Maicol Bentancor <maicol.bentancor@gmail.com>
Date:   Tue Dec 8 16:57:19 2020 -0300

    Enable rails 6.1 (lynndylanhurley#1446)

commit 575272a
Author: Ali Deishidi <sizief@gmail.com>
Date:   Fri Dec 4 19:20:56 2020 +0330

    Update faq.md (lynndylanhurley#1439)

    Type was missing

commit 2a83ef9
Author: dominikdarnel <dominik.darnel@gmail.com>
Date:   Sun Nov 8 21:51:36 2020 +0100

    Update overrides.md (lynndylanhurley#1437)

    Overrideable methods RegistrationsController#render_destroy_success and RegistrationsController#render_destroy_error were missing from docs.

commit 5ef7197
Author: Avrohom Katz <iambpentameter@gmail.com>
Date:   Sun Nov 1 10:40:30 2020 -0500

    make DTA respect controller overrides it doesn't kow about (lynndylanhurley#1435)

commit 24da31e
Author: Paulo Ribeiro <plribeiro3000@gmail.com>
Date:   Sun Nov 1 12:39:35 2020 -0300

    feat(Route): Accept override (lynndylanhurley#1432)

commit 530b4ba
Author: JP Rosevear <jprosevear@gmail.com>
Date:   Mon Oct 19 17:32:04 2020 -0400

    Remove unnecessary require_relative the file should be autoloaded (lynndylanhurley#1433)

commit 576d0b0
Author: Martin Jaime <50113670+martinjaimem@users.noreply.github.com>
Date:   Mon Oct 19 18:31:44 2020 -0300

    Enhancement: remove unnecessary statement in destroy session (lynndylanhurley#1431)

commit 0c369d6
Author: Maicol Bentancor <maicol.bentancor@gmail.com>
Date:   Sun Oct 4 17:34:44 2020 -0300

    Serialize updated at without to s (lynndylanhurley#1423)

    * working 4.2 version

    * cleanup

    * Lock simplecov to last supported cc version

    codeclimate/test-reporter#413

    Co-authored-by: Brian Dunn <brianpatrickdunn@gmail.com>

commit bb3e3f0
Author: Brent Dearth <brent.dearth@gmail.com>
Date:   Tue Sep 1 11:05:33 2020 -0600

    chore(deps): remove Sprockets requirement (lynndylanhurley#1428)

commit bef1b0b
Author: ngouy <nathangouy@free.fr>
Date:   Sat Jul 25 14:43:00 2020 -0400

    missing space in migration generator (lynndylanhurley#1422)

    So everything is nicely aligned if you uncomment this

commit b409997
Author: Maicol Bentancor <maicol.bentancor@gmail.com>
Date:   Sun Jun 21 21:53:29 2020 -0300

    Avoid failing on undefined variable for invalid record (lynndylanhurley#1416)

commit 30a1aff
Author: Maicol Bentancor <maicol.bentancor@gmail.com>
Date:   Sun Jun 21 21:52:43 2020 -0300

    Avoid nullyfing token when current_user is called before (lynndylanhurley#1417)

commit ae3547c
Author: Charlie Hua <compassion.wisdom@gmail.com>
Date:   Sun Jun 21 04:37:15 2020 +0800

    Memorize current_#{group_name} to avoid error (lynndylanhurley#722)

    Currently current_#{group_name} (ex. current_member) is not memorized as current_#{mapping} (ex. current_user) is.
    Instead, it calls set_user_by_token every time, and this could cause error if current_member is called after token is changed (maybe by some other request) and return nil

commit 849c244
Author: Maicol Bentancor <maicol.bentancor@gmail.com>
Date:   Sat Jun 20 00:39:42 2020 -0300

    Fix parent class name generator for rails 6 (lynndylanhurley#1414)

commit 449a0be
Author: Maicol Bentancor <maicol.bentancor@gmail.com>
Date:   Fri Jun 19 20:09:26 2020 -0300

    Reduce config variables doc table width (lynndylanhurley#1413)

commit 4461a09
Author: Hai Phan Nguyen <pnghai@gmail.com>
Date:   Sat Jun 20 05:27:45 2020 +0700

    Fix crash on devise 4.7.2+ (lynndylanhurley#1412)

    * Ignore sync uid in case confirmable mail changed

    * Update user_omniauth_callbacks.rb

    * Update user_omniauth_callbacks.rb

    * fix crash on devise 4.7.2+

commit bb09616
Author: Hai Phan Nguyen <pnghai@gmail.com>
Date:   Wed Jun 10 20:56:28 2020 +0700

    Ignore sync uid in case confirmable mail changed (lynndylanhurley#1407)

    * Ignore sync uid in case confirmable mail changed

    * Update user_omniauth_callbacks.rb

    * Update user_omniauth_callbacks.rb

commit b3d53b6
Author: Maicol Bentancor <maicol.bentancor@gmail.com>
Date:   Tue Jun 2 15:07:22 2020 -0300

    Bump version to 1.1.4 (lynndylanhurley#1406)

commit 2a01f4f
Author: Jamal Mohammed <jamal@mdjamal.com>
Date:   Tue Jun 2 22:07:49 2020 +0530

    Update faq.md (lynndylanhurley#1401)

    Updated FAQ to add **allow_password_change** column if not already added in existing User model.

commit 387306a
Author: Sai Chander <saichander17@gmail.com>
Date:   Wed Apr 29 08:43:10 2020 +0800

    Issue - 1358 Argument error when converting token updated_at using to_time fixed (lynndylanhurley#1388)

commit a3595f4
Author: David Loukidelis <david@loukidelis.com>
Date:   Tue Apr 28 20:41:32 2020 -0400

    Update omniauth_callbacks_controller.rb (lynndylanhurley#1398)

commit ef965d7
Author: Arun Kumar Mohan <arunmohandm@gmail.com>
Date:   Tue Apr 28 19:39:44 2020 -0500

    Fix grammar (lynndylanhurley#1396)

commit ea697f1
Author: H.Sada <47932189+h-sada@users.noreply.github.com>
Date:   Wed Apr 29 09:39:27 2020 +0900

    fixed not_email in ja.yml (lynndylanhurley#1395)

commit 275da3c
Author: Gabriel Bursztein <gabrielbursztein94@gmail.com>
Date:   Mon Mar 30 08:23:22 2020 -0300

    Fix: Save user authentication token after email confirmation (lynndylanhurley#1391)

commit 4a103ed
Author: Ahmed Magdy <ahmedmagdy711@users.noreply.github.com>
Date:   Mon Mar 30 13:19:16 2020 +0200

    Validate that token is valid for patch request last token (lynndylanhurley#1386)

commit 4d2d702
Author: Leo Kiiski <goalaleo@gmail.com>
Date:   Mon Mar 30 14:18:54 2020 +0300

    Fix token-type header key in testing example (lynndylanhurley#1390)

commit ab050c2
Author: Dylan Lederle-Ensign <dylan.lederle.ensign@gmail.com>
Date:   Mon Mar 30 07:17:57 2020 -0400

    Fix broken link (lynndylanhurley#1392)

commit 8c6ea5a
Author: Olle Jonsson <olle.jonsson@gmail.com>
Date:   Mon Mar 30 13:16:53 2020 +0200

    CI build fix: Pin to pry < 0.13 for 2.3 support, workaround CodeClimate reporter issue (lynndylanhurley#1393)

    * gemfiles: Pin to < 0.13 for 2.3 support

    * Workaround: CodeClimate not supporting SimpleCov

      - 0.18 gives us issues
      - see codeclimate/test-reporter#418

commit 49e7203
Author: K-Sato <32632542+K-Sato1995@users.noreply.github.com>
Date:   Sat Feb 29 08:17:59 2020 +0900

    Fix docs/usage/reset_password.md (lynndylanhurley#1382)

commit 55fcd11
Author: Bartek Fijałkowski <brateq@gmail.com>
Date:   Thu Jan 30 21:35:50 2020 +0100

    Add docs for confirmation endpoint (lynndylanhurley#1365)

commit e0d8dfa
Author: sugiken <ynmk1995@gmail.com>
Date:   Fri Jan 31 05:35:31 2020 +0900

    Remove Trackable option from generator (lynndylanhurley#1362)

commit 8326cc7
Author: Bartek Fijałkowski <brateq@gmail.com>
Date:   Thu Jan 30 21:33:45 2020 +0100

    Add rails 6.0 config to travis (lynndylanhurley#1366)

    * Add rails 6.0 config to travis

    * Exclude Travis Rails 6.0 checks for Ruby below 2.5

    * Updates tests to be compatible with Rails 6

    Rails 6 no longer generates migration with id: :uuid, so I changed the range of two ifs in tests and everything goes well now.

    * Add Ruby 2.7.0 to Travis

    * Exclude travis test for Ruby 2.7.0 with Rails 4.2

    Ruby 2.7 comes with Bundler 2 which is not compatible with Rails 4

    * Add to Travis test for rails 6 + mongoid 7

    * Downgrade mongoid-locker for rails 6 gemfile

    * Allow to change primary key type for Rails 6

commit 880a249
Author: Paweł Łuczak <woochaq@gmail.com>
Date:   Thu Jan 30 21:32:47 2020 +0100

    Fix missing polish and portugese translation errors (lynndylanhurley#1377)

commit 3680fd3
Author: Clément Prod'homme <prodhomme.clement@hotmail.fr>
Date:   Wed Jan 29 05:42:35 2020 +0100

    chore(docs): write complete path for authentication_test_spec (lynndylanhurley#1376)

commit 00d7abc
Author: Nicholas Martin <nicholas.martin@marketdojo.com>
Date:   Thu Jan 9 13:23:36 2020 +0000

    Add case sensitive option required to prevent deprecation warning in rails 6 (lynndylanhurley#1368)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants