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

Restore and support with solidus starter frontend #127

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/stylesheets/spree/frontend/solidus_social.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
*= require spree/frontend
*= require spree/frontend/fontello
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain this one, please?

Copy link
Author

@rahulsingh321 rahulsingh321 Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kennyadsl I faced an issue after installing the extension i.e - somehow the sprockets is looking for the file spree/frontend to be there when compiling assets, and raised an error with Sprockets::FileNotFound. I have to change this so that it points to an file instead of frontend directory as Sprockets is unable to find.

Here is the screenshot:
Screenshot-from-2024-12-24-21-56-42-12-24-2024_10_15_PM

Let me know if there any better way to handle this.

*= require_tree .
*/
11 changes: 7 additions & 4 deletions app/controllers/spree/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ def provides_callback_for(*providers)
end
end

Spree::SocialConfig.providers.keys.each do |provider|
provides_callback_for provider
SolidusSocial::OAUTH_PROVIDERS.each do |provider|
provides_callback_for provider[1].to_sym
end

def omniauth_callback
if request.env['omniauth.error'].present?
flash[:error] = I18n.t('devise.omniauth_callbacks.failure', kind: auth_hash['provider'], reason: I18n.t('spree.user_was_not_valid'))
redirect_back_or_default(root_url)
redirect_back_or_default(spree.login_path)
return
end

authentication = Spree::UserAuthentication.find_by(provider: auth_hash['provider'], uid: auth_hash['uid'])

if authentication.present? && authentication.try(:user).present?
flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: auth_hash['provider'])
sign_in_and_redirect :spree_user, authentication.user
Expand Down Expand Up @@ -68,4 +67,8 @@ def passthru
def auth_hash
request.env['omniauth.auth']
end

def after_sign_in_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || spree.account_path
end
end
2 changes: 1 addition & 1 deletion app/controllers/spree/user_authentications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class Spree::UserAuthenticationsController < Spree::StoreController
class Spree::UserAuthenticationsController < Spree::BaseController
def index
@authentications = spree_current_user.user_authentications if spree_current_user
end
Expand Down
3 changes: 2 additions & 1 deletion app/decorators/models/solidus_social/spree/user_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def self.prepended(base)
end

def apply_omniauth(omniauth)
if omniauth.fetch('info', {})['email'].present?
skip_signup_providers = SolidusSocial::OAUTH_PROVIDERS.map { |p| p[1] if p[2] == 'true' }.compact
if skip_signup_providers.include? omniauth['provider']
self.email = omniauth['info']['email'] if email.blank?
end
user_authentications.build(provider: omniauth['provider'], uid: omniauth['uid'])
Expand Down
15 changes: 8 additions & 7 deletions app/models/spree/authentication_method.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# frozen_string_literal: true

class Spree::AuthenticationMethod < ApplicationRecord
def self.provider_options
SolidusSocial.configured_providers.map { |provider_name| [provider_name.split("_").first.camelize, provider_name] }
end

validates :provider, presence: true
class Spree::AuthenticationMethod < ActiveRecord::Base
validates :provider, :api_key, :api_secret, presence: true

def self.active_authentication_methods?
where(environment: ::Rails.env, active: true).exists?
end

scope :available_for, lambda { |user|
sc = where(environment: ::Rails.env)
sc = sc.where(['provider NOT IN (?)', user.user_authentications.map(&:provider)]) if user && !user.user_authentications.empty?
sc = sc.where.not(provider: user.user_authentications.pluck(:provider)) if user && !user.user_authentications.empty?
sc
}

def provider_name
provider = SolidusSocial::OAUTH_PROVIDERS.find { |oauth_provider| oauth_provider[1] == self.provider }
provider ? provider[0].capitalize : nil
end
end
2 changes: 1 addition & 1 deletion app/models/spree/user_authentication.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class Spree::UserAuthentication < ApplicationRecord
class Spree::UserAuthentication < ActiveRecord::Base
belongs_to :user
end
63 changes: 43 additions & 20 deletions app/views/spree/admin/authentication_methods/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
<div data-hook="admin_social_methods_form_fields" class="row">
<div class="col-4">
<div data-hook="environment" class="field">
<%= f.field_container :environment do %>
<%= label_tag nil, Spree::AuthenticationMethod.human_attribute_name(:environment) %>
<%= collection_select(:authentication_method, :environment, Rails.configuration.database_configuration.keys, :to_s, :titleize, {}, { class: 'select2 fullwidth' }) %>
<% end %>
<div data-hook="admin_social_methods_form_fields">
<div class="row">
<div class="col-12 col-lg-6">
<div data-hook="environment" class="field">
<%= f.field_container :environment do %>
<%= label_tag nil, Spree::AuthenticationMethod.human_attribute_name(:environment) %>
<%= collection_select(:authentication_method, :environment, Rails.configuration.database_configuration.keys, :to_s, :titleize, {}, { class: 'select2 fullwidth' }) %>
<% end %>
</div>
</div>

<div class="col-12 col-lg-6">
<div data-hook="environment" class="field">
<%= f.field_container :provider do %>
<%= f.label :provider, I18n.t('spree.social_provider') %>
<%= f.select :provider,
SolidusSocial::OAUTH_PROVIDERS.collect { |p| [ p[0], p[1] ] },{},
{ include_blank: false, class: 'select2' } %>
<% end %>
</div>
</div>
</div>
<div class="col-4">
<div data-hook="environment" class="field">
<%= f.field_container :provider do %>
<%= f.label :provider, I18n.t('spree.social_provider') %>
<%= f.select :provider, Spree::AuthenticationMethod.provider_options, {}, { include_blank: false, class: 'select2 fullwidth' } %>
<% end %>

<div class="row">
<div class="col-12 col-lg-6">
<div data-hook="environment" class="form-group">
<%= f.label :api_key, I18n.t('spree.social_api_key') %>
<%= f.text_field :api_key, class: 'form-control' %>
</div>
</div>
<div class="col-12 col-lg-6">
<div data-hook="environment" class="form-group">
<%= f.label :api_secret, I18n.t('spree.social_api_secret') %>
<%= f.text_field :api_secret, class: 'form-control' %>
</div>
</div>
</div>
<div class="col-4">
<div data-hook="environment" class="field">
<%= f.field_container :active do %>
<%= f.label :active, I18n.t('spree.active') %><br>
<%= f.radio_button :active, :true %><span style="padding:0 2px;"><%= I18n.t('spree.say_yes') %></span>
<%= f.radio_button :active, :false %><span style="padding:0 2px"><%= I18n.t('spree.say_no') %></span>
<% end %>

<div class="row">
<div class="col-12 col-lg-6">
<div data-hook="environment" class="field">
<%= f.field_container :active do %>
<%= f.label :active, I18n.t('spree.active') %><br>
<%= f.radio_button :active, :true %><span style="padding:0 2px;"><%= I18n.t('spree.say_yes') %></span>
<%= f.radio_button :active, :false %><span style="padding:0 2px"><%= I18n.t('spree.say_no') %></span>
<% end %>
</div>
</div>
</div>
</div>
27 changes: 27 additions & 0 deletions app/views/spree/starter_frontend/shared/_social.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div id="social-signin-links" class="text-center">
<% if (!spree_current_user || !spree_current_user.user_authentications) && Spree::AuthenticationMethod.active_authentication_methods? %>
<div class="flex items-center my-4">
<hr class="w-full h-px bg-gray-200 border-0 dark:bg-gray-700">
<span class="px-3 text-gray-900 font-medium dark:text-white dark:bg-gray-900"><%= I18n.t('spree.or') %></span>
<hr class="w-full h-px bg-gray-200 border-0 dark:bg-gray-700">
</div>
<% end %>

<div class="flex flex-col items-center w-full space-y-4">
<% Spree::AuthenticationMethod.available_for(spree_current_user).each do |method| %>
<% if method.active %>
<%= form_tag(spree.send("spree_user_#{method.provider}_omniauth_authorize_path", r: rand), method: 'post', class: "w-full") do %>
<%= button_tag(type: 'submit', title: t('spree.sign_in_with', provider: method.provider_name), class: "w-full px-7 rounded-full text-body-sm font-bold leading-none uppercase whitespace-nowrap transiton-colors duration-200 border text-white flex items-center justify-between") do %>
<svg class="w-5 h-5 fill-black dark:fill-white">
<use xlink:href="<%= image_path('remixicon.symbol.svg') %>#ri-<%= method.provider_name.downcase %>-fill"></use>
</svg>
<div class="flex-1 py-3">
<span class="font-medium text-black dark:text-white text-center"><%= t('spree.sign_in_with', provider: method.provider_name) %></span>
</div>
<div></div>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
</div>
18 changes: 18 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

SolidusSocial::OAUTH_PROVIDERS.each do |provider|
SolidusSocial.init_provider(provider[1])
end

OmniAuth.config.logger = Logger.new(STDOUT)
OmniAuth.logger.progname = 'omniauth'

OmniAuth.config.on_failure = proc do |env|
env['devise.mapping'] = Devise.mappings[Spree.user_class.table_name.singularize.to_sym]
controller_klass = ActiveSupport::Inflector.constantize("Spree::OmniauthCallbacksController")
controller_klass.action(:failure).call(env)
end

Devise.setup do |config|
config.router_name = :spree
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
path: Spree::SocialConfig[:path_prefix]
resources :user_authentications

get 'account' => 'users#show', as: 'user_root'
get 'account', to: 'users#show', as: 'user_root'

namespace :admin do
resources :authentication_methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AddDbIndexToUserAuthentications < SolidusSupport::Migration[4.2]
def change
add_index :spree_user_authentications, [:uid, :provider], unique: true
add_index :spree_user_authentications, :user_id
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddForeignKeyToUserAuthenticationsForUserId < SolidusSupport::Migration[4.2]
def change
add_foreign_key :spree_user_authentications, :spree_users, column: :user_id
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ChangeUserIdTypeForSpreeUserAuthentications < SolidusSupport::Migration[4.2]
def change
change_column :spree_user_authentications, :user_id, :bigint
end
end
6 changes: 0 additions & 6 deletions lib/generators/solidus_social/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module SolidusSocial
module Generators
class InstallGenerator < Rails::Generators::Base
source_root "#{__dir__}/templates"

class_option :auto_run_migrations, type: :boolean, default: false

def add_stylesheets
Expand All @@ -15,10 +13,6 @@ def add_migrations
run 'bin/rails railties:install:migrations FROM=solidus_social'
end

def copy_initializer
template "config/initializers/solidus_social.rb"
end

def run_migrations
run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]'))
if run_migrations
Expand Down

This file was deleted.

31 changes: 24 additions & 7 deletions lib/solidus_social.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,37 @@
require 'solidus_social/version'
require 'solidus_social/engine'


module SolidusSocial
def self.configured_providers
::Spree::SocialConfig.providers.keys.map(&:to_s)
end
OAUTH_PROVIDERS = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what's wrong with configured_providers? Why do we need to change it into a constant?

Copy link
Author

@rahulsingh321 rahulsingh321 Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kennyadsl Earlier, we used to configure the providers using an initializer file, where we configure provider with credentials, I find it would be more appropriate if we pull the provider and credentials details from the Spree::AuthenticationMethod table instead, so I removed this method.

Constant I took, just to maintain the list of provider into the extension itself.
Let me know your thoughts on this.

%w(Facebook facebook true),
%w(Twitter twitter2 false),
%w(Github github true),
%w(Google google_oauth2 true)
]

# Setup all OAuth providers
def self.init_provider(provider)
begin
ActiveRecord::Base.connection_pool.with_connection(&:active?)
rescue
return
end

def self.init_providers
::Spree::SocialConfig.providers.each do |provider, credentials|
setup_key_for(provider, credentials[:api_key], credentials[:api_secret])
return unless ActiveRecord::Base.connection.data_source_exists?('spree_authentication_methods')
key, secret = nil
::Spree::AuthenticationMethod.where(environment: ::Rails.env).each do |auth_method|
next unless auth_method.provider == provider
key = auth_method.api_key
secret = auth_method.api_secret
Rails.logger.info("[Solidus Social] Loading #{auth_method.provider.capitalize} as authentication source")
end
setup_key_for(provider.to_sym, key, secret)
end

def self.setup_key_for(provider, key, secret)
Devise.setup do |config|
config.omniauth provider, key, secret, setup: true
config.omniauth provider, key, secret, setup: true, info_fields: 'email, name'
end
end
end
9 changes: 9 additions & 0 deletions lib/solidus_social/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'spree/core'
require 'solidus_social/config'
require 'solidus_social/facebook_omniauth_strategy_ext'
require 'solidus_social/solidus_support'

module SolidusSocial
class Engine < Rails::Engine
Expand All @@ -18,6 +19,14 @@ class Engine < Rails::Engine

engine_name 'solidus_social'

initializer 'solidus_social.environment', before: 'spree.environment' do
AUTHENTICATION_METHOD_PATH = config.root.join(
"app/models/spree/authentication_method.rb"
).to_s

load AUTHENTICATION_METHOD_PATH
end

# use rspec for tests
config.generators do |g|
g.test_framework :rspec
Expand Down
1 change: 0 additions & 1 deletion lib/solidus_social/social_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Spree
class SocialConfiguration < Preferences::Configuration
attr_accessor :providers
preference :path_prefix, :string, default: 'users'

::Spree::SocialConfig = Spree::SocialConfiguration.new
Expand Down
9 changes: 9 additions & 0 deletions lib/solidus_social/solidus_support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SolidusSupport
class << self
# Currently it checks only legacy frontend support
# enabling this here for starter frontend to load missing routes and controllers
def frontend_available?
true
end
end
end
Loading