From cd98088e7f406ef40ec15962cbf0c36418d420df Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 26 Mar 2013 17:22:25 +0400 Subject: [PATCH] #30: find user by email before registration --- Gemfile | 2 -- .../users/omniauth_callbacks_controller.rb | 18 +++++++++--------- app/models/user.rb | 14 -------------- config/initializers/devise.rb | 2 +- config/initializers/omniauth.rb | 8 -------- 5 files changed, 10 insertions(+), 34 deletions(-) diff --git a/Gemfile b/Gemfile index 4b7317e2b..99c57eb49 100644 --- a/Gemfile +++ b/Gemfile @@ -7,13 +7,11 @@ gem 'pg', '~> 0.14.0' gem 'devise', '~> 2.2.3' gem 'omniauth' -# gem 'oa-oauth', :require => 'omniauth/oauth' gem 'omniauth-facebook' gem 'omniauth-google-oauth2' gem 'omniauth-github' # gem 'omniauth-openid', '~> 1.0.1' gem 'cancan', '1.6.7' # 1.6.8 fail specs with strange error -# gem 'uuidtools' gem 'ancestry', '~> 1.3.0' gem 'paperclip', '~> 3.3.1' diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 99cad3cf7..4e13d8833 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -47,18 +47,18 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController email = auth['info']['email'] when 'github' name = auth['info']['nickname'] - email = auth['info']['email'] || "#{name}@github.com" + email = auth['info']['email'] else raise 'Provider #{provider} not handled' end - user = User.create!( - :uname => "#{provider.gsub(/_oauth2/,'')}_#{uid}", - :name => name, - :email => email, - :password => Devise.friendly_token[0,20] - ) - user.confirmed_at = Time.zone.now - user.save + user = User.find_or_initialize_by_email(email) + if user.new_record? + user.name = name + user.uname = name.gsub(/\s/, '').underscore + user.password = Devise.friendly_token[0,20] + user.confirmed_at = Time.zone.now + user.save + end authentication.user = user end authentication.save! diff --git a/app/models/user.rb b/app/models/user.rb index fce41e6b6..71854e991 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -99,20 +99,6 @@ class User < Avatar { :value => login.downcase, :orig_value => login }]).first end - def new_with_session(params, session) - super.tap do |user| - if data = session["devise.omniauth_data"] - if info = data['info'] and info.present? - user.email = info['email'].presence if user.email.blank? - user.uname ||= info['nickname'].presence || info['username'].presence - user.name ||= info['name'].presence || [info['first_name'], info['last_name']].join(' ').strip - end - user.password = Devise.friendly_token[0,20] # stub password - user.authentications.build :uid => data['uid'], :provider => data['provider'] - end - end - end - def auth_by_token_or_login_pass(user, pass) u = User.find_for_database_authentication(:login => user) u if u && !u.access_locked? && (u.authentication_token == user || u.valid_password?(pass)) diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index bc4951c40..a0b635a4f 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -194,7 +194,7 @@ Devise.setup do |config| config.omniauth :facebook, APP_CONFIG['keys']['facebook']['id'], APP_CONFIG['keys']['facebook']['secret'] config.omniauth :google_oauth2, APP_CONFIG['keys']['google']['id'], APP_CONFIG['keys']['google']['secret'], {:access_type => 'offline', :approval_prompt => ''} - config.omniauth :github, APP_CONFIG['keys']['github']['id'], APP_CONFIG['keys']['github']['secret'] + config.omniauth :github, APP_CONFIG['keys']['github']['id'], APP_CONFIG['keys']['github']['secret'], {:scope => 'user'} # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index ac5e72a76..d5f26a3d8 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -1,9 +1 @@ -# require "omniauth-facebook" - -# Rails.application.config.middleware.use OmniAuth::Builder do -# [:facebook, :github, :google_oauth2].each do |kind| -# provider kind, APP_CONFIG['keys']["#{kind}"]['id'], APP_CONFIG['keys']["#{kind}"]['secret'] -# end -# end - OmniAuth.config.logger = Rails.logger \ No newline at end of file