-
Notifications
You must be signed in to change notification settings - Fork 282
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
base: master
Are you sure you want to change the base?
API for OAUTH Login #245
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Spree::Api::V1::OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] |
||
authentication = Spree::UserAuthentication.find_by_provider_and_uid(auth_hash['provider'], auth_hash['uid']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [90/80] |
||
end | ||
end | ||
render json: @token_response.body, status: 200 | ||
end | ||
|
||
def access_token(user) | ||
access_token = Doorkeeper::AccessToken.create!({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']) }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [82/80] |
||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
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]