module DoorkeeperHub module Helpers extend ActiveSupport::Concern included do before_action :authenticate_doorkeeper! end def authenticate_doorkeeper! if signed_in?(DoorkeeperHub.devise_scope) redirect_to root_path_with_token if params[:d_token].nil? else redirect_to omniauth_authorize_path( DoorkeeperHub.devise_scope, :saml ) end end def default_url_options(options = {}) options[:d_token] = params[:d_token] options end def after_sign_in_path_for(_) root_path_with_token end def root_path_with_token root_path d_token: params_or_default_d_token end def params_or_default_d_token params[:d_token] || current_user.companies.first.doorkeeper_token end def company @company ||= current_user.companies.active.find_by(doorkeeper_token: params[:d_token]) || raise(ActionController::RoutingError.new('Not Found')) end end end