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

NoMethodError: undefined method `id' for nil:NilClass "devise_masquerade_#{masqueraded_resource_name}_#{masqueradable_resource.id}_#{guid}".to_sym #140

Open
rodriguevara opened this issue Feb 19, 2024 · 13 comments

Comments

@rodriguevara
Copy link

Hi guys, thanks for mantaining this gem! I'm getting this error:

NoMethodError: undefined method `id' for nil:NilClass
    "devise_masquerade_#{masqueraded_resource_name}_#{masqueradable_resource.id}_#{guid}".to_sym

sometimes when trying to log in. I'm using the masquerade_path method. It happens sometimes, and I couldn't find a 'pattern' so to speak. Most of the times works, but with some users this error comes up. Any ideas? Thanks a lot!

@adamjleonard
Copy link

Just started happening out of the blue for me as well

@rodriguevara
Copy link
Author

@adamjleonard did you find a solution?

@adamjleonard
Copy link

@rodriguevara I did not

@oivoodoo
Copy link
Owner

Hi @adamjleonard and @rodriguevara

have you tried to switch cache to session storage method?

    Devise.masquerade_storage_method = :session

@rodriguevara
Copy link
Author

Hi @oivoodoo , thanks for replying. Where should this code go?

@oivoodoo
Copy link
Owner

I don't remember Rails enough :0 but I believe in place where you have devise initializer.

@rodriguevara
Copy link
Author

@oivoodoo That didnt help, but I noticed that the error occurs after a minute of staying in the view where I call the masquerade, let's say my users index. If a minute pass without me doing anything else, then when trying to masquerade, the error comes up. So maybe it has something to do with @@masquerade_expires_in = 1.minute?

@oivoodoo
Copy link
Owner

oivoodoo commented Apr 3, 2024

@rodriguevara it's the normal behaviour, you can increase the time but lets say if your admin went for coffee and keep the page opened with masquerade access, we need to make possible to skip ability to anyone to do the authentication.

@rodriguevara
Copy link
Author

@oivoodoo yep, that makes sense, it would be helpful a rescue for that error, maybe redirecting to root_path or something like that

@umaimaz123
Copy link

@rodriguevara did this help you or you find the right solution?

@rodriguevara
Copy link
Author

@umaimaz123 @@masquerade_expires_in = 1.minute you need to increase the time. 10 minutes works just fine for me

@remy727
Copy link

remy727 commented Oct 8, 2024

My solution:

  • Step1: Add id query param on view like masquerade_path(user, id: user.id).
  • Step 2: Override find_masqueradable_resource method.
def find_masqueradable_resource
  GlobalID::Locator.locate_signed(params[Devise.masquerade_param], for: 'masquerade') ||
    masqueraded_resource_class.find(params[:id])
end

@oivoodoo
Copy link
Owner

oivoodoo commented Oct 9, 2024

Hi @remy727 .

Could you add some notes why it would help? would it be possible to include as feature scenario / spec to the library your changes as enhancements? probably it would help the other developers as well.

thank you!

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

No branches or pull requests

5 participants