Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Commit

Permalink
unique social registration
Browse files Browse the repository at this point in the history
  • Loading branch information
max-reznichenko committed Jun 25, 2012
1 parent ccbd8c2 commit e857533
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
6 changes: 5 additions & 1 deletion app/controllers/users/omniauth_callbacks_controller.rb
Expand Up @@ -10,7 +10,11 @@ def facebook
def vkontakte
current_user = User.oauth_find_or_create(:vkontakte, request.env['omniauth.auth'])
sign_in current_user
redirect_to current_user.skills.blank? ? interests_path : user_path(current_user)
if current_user.email.ends_with?('@vk.com')
redirect_to user_update_email_path(current_user)
else
redirect_to current_user.skills.blank? ? interests_path : user_path(current_user)
end
end

def failure
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/users_controller.rb
Expand Up @@ -25,4 +25,8 @@ def map_interest
end
respond_with 1
end

def update_email

end
end
40 changes: 21 additions & 19 deletions app/models/user.rb
Expand Up @@ -51,28 +51,30 @@ def upcoming_student_lessons
end

def self.oauth_find_or_create(provider, auth)
# TODO
# Make sure the authorization is unique
begin
UserRegistration.where(provider: provider.to_s.downcase, provider_user_id: auth.uid).first.user
rescue
user = User.create(
email: provider == :vkontakte ? "#{auth.uid}@vk.com" : auth.info.email,
first_name: auth.info.first_name,
last_name: auth.info.last_name,
sex: provider == :vkontakte ? VKONTAKTE_SEX_ASSOCIATIONS[auth.extra.raw_info.sex.to_i] : auth.extra.raw_info.gender,
password: 'fake_password',
password_confirmation: 'fake_password',
send_emails: true
)
user.user_registrations.create(
provider: provider.to_s.downcase,
provider_user_id: auth.uid,
hash_token: auth.credentials.token,
provider_url: auth.info.urls[provider.downcase.to_s.titleize]
)
user.image_attachment = ImageAttachment.create(image: ImageAttachment.image_from_url(auth.info.image, auth.uid))
user
begin
User.find_by_email(provider == :vkontakte ? "#{auth.uid}@vk.com" : auth.info.email) or raise
rescue
user = User.create(
email: provider == :vkontakte ? "#{auth.uid}@vk.com" : auth.info.email,
first_name: auth.info.first_name,
last_name: auth.info.last_name,
sex: provider == :vkontakte ? VKONTAKTE_SEX_ASSOCIATIONS[auth.extra.raw_info.sex.to_i] : auth.extra.raw_info.gender,
password: 'fake_password',
password_confirmation: 'fake_password',
send_emails: true
)
user.user_registrations.create(
provider: provider.to_s.downcase,
provider_user_id: auth.uid,
hash_token: auth.credentials.token,
provider_url: auth.info.urls[provider.downcase.to_s.titleize]
)
user.image_attachment = ImageAttachment.create(image: ImageAttachment.image_from_url(auth.info.image, auth.uid))
user
end
end
end

Expand Down
Empty file.
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -5,6 +5,7 @@

resources :users, only: %w(show edit update) do
get "map_interest/:sub_interest_id", to: :map_interest, as: :map_interest
get "update-email", to: :update_email, as: :update_email
end
resources :lessons, only: %w(show edit update create new index) do
get "new-course", on: :collection, to: :new_course, as: :new_course
Expand Down
6 changes: 0 additions & 6 deletions db/migrate/20120622150805_add_providers_user_id_to_users.rb

This file was deleted.

0 comments on commit e857533

Please sign in to comment.