ruby-on-railsruby-on-rails-5clearance

Rails 5 Geolocation with Clearance


I'm currently using Clearance for authentication. As part of the login process I want to ensure that I have a location for the user (stored in the session).

My question is how to do this in the context of clearance? Ideally I only want to perform the lookup on login (to save network traffic / API calls). If I can't locate a user then I'll deny login.

I was thinking of using a guard but I don't seem to have access to request.ip or session which is a bit of a deal breaker. I was also trying to avoid redirecting to a URL that only does the geolocation and then redirects again.

Anybody have ideas on a nice model on how to make this work? Thanks!


Solution

  • Sign in guards are mostly intended for processes that will prevent or allow sign in. I don't think they are a good fit here. For this use case, I would suggest overriding sign_in, which is generally mixed in to ApplicationController

    def sign_in(user, &block)
      super
    
      if signed_in?
        UserGeocoder.call(current_user)
      end
    end