#30: find user by email before registration

This commit is contained in:
Vokhmin Alexey V 2013-03-26 17:22:25 +04:00
parent abfe502b5f
commit cd98088e7f
5 changed files with 10 additions and 34 deletions

View File

@ -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'

View File

@ -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!

View File

@ -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))

View File

@ -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

View File

@ -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