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

API for OAUTH Login #245

Open
wants to merge 3 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
48 changes: 48 additions & 0 deletions app/controllers/spree/api/v1/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Spree::Api::V1::OmniauthCallbacksController < Devise::OmniauthCallbacksController

Choose a reason for hiding this comment

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

Style/Documentation: Missing top-level class documentation comment.
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
Style/ClassAndModuleChildren: Use nested module/class definitions instead of compact style.
Metrics/LineLength: Line is too long. [87/80]


Choose a reason for hiding this comment

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

Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning.

skip_before_action :verify_authenticity_token
before_action :validate_provider, only: :login

def login

Choose a reason for hiding this comment

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

Metrics/AbcSize: Assignment Branch Condition size for login is too high. [32.62/15]
Metrics/CyclomaticComplexity: Cyclomatic complexity for login is too high. [7/6]
Metrics/MethodLength: Method has too many lines. [17/10]
Metrics/PerceivedComplexity: Perceived complexity for login is too high. [9/7]

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

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [112/80]


if authentication.present? && authentication.try(:user).present?
access_token(authentication.user)
elsif spree_current_user
spree_current_user.apply_omniauth(auth_hash)
spree_current_user.save!
access_token(spree_current_user)
else
user = Spree::User.find_by_email(auth_hash['info']['email']) || Spree::User.new

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [85/80]

user.apply_omniauth(auth_hash)
if user.save
access_token(user).body
else
render json: { error: I18n.t('spree.user_was_not_valid') }, status: 422 and return

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [90/80]
Style/AndOr: Use && instead of and.

end
end
render json: @token_response.body, status: 200
end

def access_token(user)
access_token = Doorkeeper::AccessToken.create!({

Choose a reason for hiding this comment

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

Style/BracesAroundHashParameters: Redundant curly braces around a hash parameter.

resource_owner_id: user.id,

Choose a reason for hiding this comment

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

Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.

expires_in: Doorkeeper.configuration.access_token_expires_in,
use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?
})

Choose a reason for hiding this comment

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

Layout/IndentHash: Indent the right brace the same as the first position after the preceding left parenthesis.

@token_response = Doorkeeper::OAuth::TokenResponse.new(access_token)
end

def auth_hash
params[:omniauth_callback]
end

def validate_provider
eligible_providers = SpreeSocial::OAUTH_PROVIDERS.map { |provider| provider[1] if provider[2] == 'true' }.compact

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [117/80]


unless eligible_providers.include?(auth_hash['provider'])

Choose a reason for hiding this comment

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

Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.

render json: { error: I18n.t('devise.omniauth_callbacks.provider_not_found', kind: auth_hash['provider']) },

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [114/80]

status: 422
end
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ en:
devise:
omniauth_callbacks:
success: "You are now signed in with your %{kind} account."
provider_not_found: "Could not authenticate you from %{kind} becasue the provider is invalid"
spree:
environment: Environment
user_was_not_valid: User was not valid.
Expand Down
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
namespace :admin do
resources :authentication_methods
end

namespace :api, defaults: { format: 'json' } do
namespace :v1 do
devise_scope :spree_user do
post '/spree_oauth/social_login/:provider', to: 'omniauth_callbacks#login'

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [82/80]

end
end
end
end