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

Using Authie in route middleware #20

Closed
stefanvermaas opened this issue Sep 6, 2019 · 3 comments
Closed

Using Authie in route middleware #20

stefanvermaas opened this issue Sep 6, 2019 · 3 comments

Comments

@stefanvermaas
Copy link

Hi there,

I've got a question regarding to using the current_user in a route constraint of Rails. I've got a route constraint in which I check the role of the authenticated user (if any) and adjust the root_path according to their role.

The code looks something like this;

class UserTypeConstraint
  def initialize(user_type)
    @user_type = user_type
  end

  def matches?(request)
    controller = Authie::RackController.new(request.env)
    current_user = controller.current_user
    current_user && current_user.send("#{@user_type}?".to_sym)
  end
end

But it seems the Authie::RackController can't be loaded. Any ideas why this happens, whether it's intended or am I just doing something wrong?

Cheers

@stefanvermaas stefanvermaas changed the title Using Authie in middleware Using Authie in route middleware Sep 6, 2019
@stefanvermaas
Copy link
Author

Small update;

Whenever I tried to implement the current logic from the Authie::RackController myself inside the class, it all seems to work. So, it might be an autoload thing.

class UserKindConstraint
  class RackController
    def initialize(request)
      @env = request.env
      @request = request
    end

    def auth_session
      @auth_session ||= Authie::Session.get_session(self)
    end

    def current_user
      auth_session.user if auth_session.is_a?(Authie::Session)
    end
  end

  def initialize(user_type)
    @user_type = user_type
  end

  # Only returns true if there is a session with a user_id in it, if the user
  # can be found, and that user has the proper kind. False in every other case.
  def matches?(request)
    controller = RackController.new(request)
    current_user = controller.current_user
    current_user && current_user.send("#{@user_type}?".to_sym)
  end
end

@skylarmacdonald
Copy link
Contributor

This is two years later, so I don't know if you're still having this problem (or even still using Authie), but I found that adding this:

require 'authie/rack_controller'

at the top of my constraint file did the trick. It doesn't look like the gem loads that class by default.

@stefanvermaas
Copy link
Author

I'm not using it anymore indeed, but thanks for providing the answer to my question @skylarmacdonald! At least it will help the community to find the answer :)!

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

2 participants