devise-passwordless icon indicating copy to clipboard operation
devise-passwordless copied to clipboard

Mongoid Support?

Open rickygu opened this issue 3 years ago • 8 comments

I followed the setup guide with devise (4.7.3) and mongoid (7.1.4)

When I click on the magic link in the email, i get the following error:

Mongoid::Errors::DocumentNotFound in Devise::Passwordless::MagicLinksController#show

message: Document not found for class User with attributes {:id=>["5f975ed8b5d8bf53e9ff90ae"]}. summary: When calling User.find_by with a hash of attributes, all attributes provided must match a document in the database or this error will be raised. resolution: Search for attributes that are in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error.

Is this due to the Gem not supporting mongoid or is there something I did wrong? It seems like the error is due to the way User.find_by is called doesn't work well with mongo?

rickygu avatar May 15 '21 21:05 rickygu

ok upon more digging. at lib/devise/strategies/magic_link_authenticatable.rb:30

resource = mapping.to.find_by(id: data["data"]["resource"]["key"])

is giving the error because mongoid is expecting a string for ID for find_by, but data["data"]["resource"]["key"] returns an Array:

>> data["data"]["resource"]["key"].class => Array

>> data["data"]["resource"]["key"] => ["5f975ed8b5d8bf53e9ff90ae"]

rickygu avatar May 15 '21 21:05 rickygu

Ok I forked the project and changed

resource = mapping.to.find_by(id: data["data"]["resource"]["key"])

to

resource = mapping.to.find_by(id: data["data"]["resource"]["key"].first)

and now it's working on my mongoid project. I'm not sure what the effect this will be on other types of database is. Do you have a test suite?

rickygu avatar May 15 '21 22:05 rickygu

Ah good catch, I should not be using .find_by there, I should be using OrmAdapter's .find_first like the database_authenticatable strategy.

I currently am not testing Mongoid but I'd like to support it, so could you tell me if this branch fixes your problem?

gem "devise-passwordless", github: "abevoelker/devise-passwordless", branch: "mongoid-fix"

abevoelker avatar May 15 '21 22:05 abevoelker

Do you have a test suite?

Not yet, pull requests welcome :smile:

abevoelker avatar May 15 '21 22:05 abevoelker

I tested your mongoid-fix branch and it is not working. getting the error: 'Invalid or expired login link." and redirecting to the login page.

I haven't had time to dig further into the reason why. I'm assuming the .find_first is not returning a user.

rickygu avatar May 15 '21 22:05 rickygu

Did you generate a fresh login link before testing? If you don’t have time to test it out don’t sweat it I’ll work on it later

On Sat, May 15, 2021 at 5:22 PM Ricky Gu @.***> wrote:

I tested your mongoid-fix branch and it is not working. getting the error: 'Invalid or expired login link." and redirecting to the login page.

I haven't had time to dig further into the reason why. I'm assuming the .find_first is not returning a user.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/abevoelker/devise-passwordless/issues/4#issuecomment-841733496, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABFO46LW6ZADYSWGOGHOZDTN3X3FANCNFSM446ITWXQ .

abevoelker avatar May 15 '21 22:05 abevoelker

yeah i did generate one. I logged out. clicked on the old link it failed. so I sent another email to myself and clicked on the new link but still no dice.

rickygu avatar May 15 '21 22:05 rickygu

All right cool, thanks for trying

On Sat, May 15, 2021 at 5:33 PM Ricky Gu @.***> wrote:

yeah i did generate one. I logged out. clicked on the old link it failed. so I sent another email to myself and clicked on the new link but still no dice.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/abevoelker/devise-passwordless/issues/4#issuecomment-841734361, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABFO45Y6XOFC3GCCGO45NLTN3ZCBANCNFSM446ITWXQ .

abevoelker avatar May 15 '21 22:05 abevoelker

Thanks for your original ticket on this issue way back in 2021! While I don't have an explicit test for Mongoid, the latest version of this gem now defaults to a token strategy that utilizes Rails's signed global IDs, which should support Mongoid out of the box as long as the GlobalID mixin is provided:

module Mongoid::Document
  include GlobalID::Identification
end

Closing this issue now as presumably fixed - feel free to reopen if needed. :bowing_man:

abevoelker avatar Sep 15 '23 22:09 abevoelker