Ask A Question

Notifications

You’re not receiving notifications from this thread.

Devise Masquerade as another User Discussion

Great video, can you explain more about the page.resources? where that is coming from and how it contains the user info for that method. Thanks

Reply

It's the resource for the current page that you're viewing inside Administrate. Just their naming convention since the admin is generic for any models you may have.

Reply

Very cool! Looking forward to seeing how to build this from scratch!

Thanks, Chris!

Reply

Excellent!

Reply

Awesome tutorial! Personally I found this plugin to be a much simpler alternative when adding this feature a few months ago, but both are very good nonetheless: https://github.com/ankane/p...

Reply

There's a bunch of great options like this. Cool thing about Pretender is that it can work with anything, but the nice part of devise_masquerade is it handles all the controllers and routes for you.

Reply

It looks like https://github.com/ankane/pretender does not let an Admin model impersonate a User model.

https://github.com/ankane/pretender/issues/55

Reply

Great episode, will be nice to have another episode with JTW and **without** Devise.

Reply

Authenticating as another user via admin console is a really nice idea. It may save a lot of time for QA. You inspired me to try something like this in one of my projects. But there is a bit different situation:

1. I have separate model for admin console users.
2. Admin console is running on a separate domain (this is the same Rails app with one common DB though).

Aparently in this case I'll have to implement custom solution instead of using devise_masquerade gem. Here's my idea:

- Authenticated admin clicks a link on admin console to sign is to primary application as some specific User.
- Application creates authentication token and saves it to DB. Something like this tuple: `AuthRequest.create(secret_token, target_user_id, token_expiration_time)` (assuming we have AuthRequest model to keep authentication requests).
- After token is persisted, admin console redirects the admin to primary application, using full URL with different domain name. `secret_token` should be one of the parameters for this request.
- Primary application validates secret token and authenticates current user with associated User records. Like so (the code is simplified):

``` ruby
class AuthRequestController
# Assuming this is an action that suppose to handle admin console redirects
def authenticate
auth_request = AuthRequest.find(param[:id])
sign_in(auth_request.target_user) # Calling Devise helper
auth_request.destroy! # Eliminating authentication request record
end
end
```

But may be there are more straightforward ways to do this. I'll be grateful if you share your opinion.

Reply

(Forgot that Disqus ignores markdown, lol.)

Reply

Hello Chris, I submitted a transcript for this episode, please review it so I can earn a free month. Thank you :)

Reply

Hi Chris, firstly, thanks a lot for your videos! They're so valuable!

My first question is related to using masquerade together with the friendly_id gem. I noticed masquerade_path(@user) is redirecting to /users/masquerade/chris, for instance. If I hardcode the user id - like in /users/masquerade/8 - it works. Any insight on how can I make it work properly?

Also, and even more important: if any user tries to open this URI, even if he's not an admin, he's able to access other users' accounts \o/ won't that happen in your application as well?

Reply

Since this is an administrative thing, you could explicitly pass in the user id like this: masquerade_path(@user.id) which should always put the numerical ID in, or you could take a look at overriding the masquerade query to use the friendly.find that is required for friendly_id lookups. I'd probably just pass in the ID explicitly since it's only accessible to admins.

And you can make sure this is accessible only for admins by doing this if you're using CanCan or putting your own before_action in the overridden controller to authorize for only admins: https://github.com/oivoodoo...

I don't think I mentioned authorizing that url in the episode like I should have. That's an important piece!

Reply

Hey guys,

For me current_user is still returning the user I am originally logged in with.
Any help appreciated. Thanks.

Reply

There seems to be an issue in the latest version of devise_masquerade. In development the workaround to enable caching worked

rails dev:cache
Reply

rails dev:cache fixed it for me. Thank you.

Reply

Would this gem be configurable for a situation where the backend is a Rails API and the frontend is a React client?

Reply

Depends on how you're doing authentication. If you're using session cookies, it'll work out of the box. Otherwise you'll need to build your own mechanism that works similarly.

Reply

Hi Chris!

I'm trying to get masquerade working with the rails_admin gem but unsure how to create the masquerade path in rails_admin? Your help would be greatly appreciated!

Reply

hey Georges-Alexandre Haines,

I was looking to do something similar and best way I found is to create a "field" in the list view of RailsAdmin for the model.

Something like this:

      field :masquerade do
        formatted_value do
          output = []
          o = bindings[:object]
          v = bindings[:view]
          output << "\<a href=\"/user/masquerade/#{o.id}\"\>Login as</a>"
          v.raw output.join("&nbsp;")
        end
      end

So you can customize the view for what you see in the model's list view in RailsAdmin by doing this in the model:

class User < ApplicationRecord
...
...

  rails_admin do
    list do
      field :id
      ...
    end
  end
end
Reply

@Rutul Dave
Is this working? I am agetting ActionController::RoutingError.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,265+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.