Skip to content

Instantly share code, notes, and snippets.

@aaronrussell
Last active February 14, 2017 21:08
Show Gist options
  • Save aaronrussell/151d89cd6ac06dae94f572cb3ab511da to your computer and use it in GitHub Desktop.
Save aaronrussell/151d89cd6ac06dae94f572cb3ab511da to your computer and use it in GitHub Desktop.
Authenticate PushType with Spree users and remove PushType users from admin UI
# app/controllers/concerns/push_type/api_authentication_methods.rb
module PushType
module ApiAuthenticationMethods
extend ActiveSupport::Concern
included do
before_action :authenticate_spree_user!
end
protected
def authenticate_spree_user!
head 401
end
end
end
# Within config/application.rb
# Add auth to PushType::Admin
config.to_prepare do
PushType::AdminController.include PushType::AuthenticationMethods
PushType::ApiController.include PushType::ApiAuthenticationMethods
end
# Remove users from PushType menu
config.after_initialize do
PushType.menu(:main).items.delete_if { |i| i.key == :users }
end
# app/controllers/concerns/push_type/authentication_methods.rb
module PushType
module AuthenticationMethods
extend ActiveSupport::Concern
included do
before_action :authenticate_spree_user!
end
protected
def push_type_user
nil
end
def authenticate_spree_user!
if spree_user_signed_in? && current_spree_user.admin?
super
else
redirect_to spree.login_path, alert: t('devise.failure.unauthenticated')
end
end
end
end
# Gemfile
# Selectively only require the parts of PushType we need
gem 'push_type_core', '~> 0.10.2'
gem 'push_type_api', '~> 0.10.2'
gem 'push_type_admin', '~> 0.10.2'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment