diff --git a/lib/generators/sorcery/templates/initializer.rb b/lib/generators/sorcery/templates/initializer.rb index 36df69de..59a542ce 100644 --- a/lib/generators/sorcery/templates/initializer.rb +++ b/lib/generators/sorcery/templates/initializer.rb @@ -107,7 +107,7 @@ # config.twitter.key = "" # config.twitter.secret = "" # config.twitter.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=twitter" - # config.twitter.user_info_mapping = {:email => "screen_name"} + # config.twitter.user_info_mapping = {:username => "screen_name"} # # config.facebook.key = "" # config.facebook.secret = "" diff --git a/lib/generators/sorcery/templates/migration/core.rb b/lib/generators/sorcery/templates/migration/core.rb index af5f721c..6ab8e929 100644 --- a/lib/generators/sorcery/templates/migration/core.rb +++ b/lib/generators/sorcery/templates/migration/core.rb @@ -2,6 +2,7 @@ class SorceryCore < <%= migration_class_name %> def change create_table :<%= model_class_name.tableize %> do |t| t.string :email, :null => false + t.string :username t.string :crypted_password t.string :salt @@ -9,5 +10,7 @@ def change end add_index :<%= model_class_name.tableize %>, :email, unique: true + add_index :<%= model_class_name.tableize %>, :username, unique: true + end end diff --git a/lib/sorcery/model/submodules/external.rb b/lib/sorcery/model/submodules/external.rb index 3a84c23b..842ec873 100644 --- a/lib/sorcery/model/submodules/external.rb +++ b/lib/sorcery/model/submodules/external.rb @@ -59,6 +59,11 @@ def create_from_provider(provider, uid, attrs) user.send(:"#{k}=", v) end + # For external services that don't offer an email like twitter. + unless attrs.has_key?(:email) && user.attributes.has_key?('email') + user.send(:'email=', "#{SecureRandom.uuid}@example.com") + end + if block_given? return false unless yield user end diff --git a/lib/sorcery/test_helpers/internal.rb b/lib/sorcery/test_helpers/internal.rb index 6f603c27..6d557dcd 100644 --- a/lib/sorcery/test_helpers/internal.rb +++ b/lib/sorcery/test_helpers/internal.rb @@ -35,23 +35,13 @@ def create_new_user(attributes_hash = nil) end def create_new_external_user(provider, attributes_hash = nil) - user_attributes_hash = attributes_hash || { username: 'gizmo' } + user_attributes_hash = attributes_hash || { username: 'gizmo', email: '' } @user = User.new(user_attributes_hash) @user.sorcery_adapter.save(raise_on_failure: true) @user.authentications.create!(provider: provider, uid: 123) @user end - def custom_create_new_external_user(provider, authentication_class, attributes_hash = nil) - authentication_association = authentication_class.name.underscore.pluralize - - user_attributes_hash = attributes_hash || { username: 'gizmo' } - @user = User.new(user_attributes_hash) - @user.sorcery_adapter.save(raise_on_failure: true) - @user.send(authentication_association).create!(provider: provider, uid: 123) - @user - end - def sorcery_model_property_set(property, *values) User.class_eval do sorcery_config.send(:"#{property}=", *values) diff --git a/spec/rails_app/db/migrate/core/20101224223620_create_users.rb b/spec/rails_app/db/migrate/core/20101224223620_create_users.rb index a542db86..400b7ecb 100644 --- a/spec/rails_app/db/migrate/core/20101224223620_create_users.rb +++ b/spec/rails_app/db/migrate/core/20101224223620_create_users.rb @@ -1,13 +1,15 @@ class CreateUsers < ActiveRecord::CompatibleLegacyMigration.migration_class def self.up create_table :users do |t| - t.string :username, null: false - t.string :email, default: nil + t.string :username + t.string :email, null: false t.string :crypted_password t.string :salt t.timestamps null: false end + add_index :users, :email, unique: true + add_index :users, :username, unique: true end def self.down diff --git a/spec/shared_examples/user_shared_examples.rb b/spec/shared_examples/user_shared_examples.rb index c563cc95..1dae3646 100644 --- a/spec/shared_examples/user_shared_examples.rb +++ b/spec/shared_examples/user_shared_examples.rb @@ -560,6 +560,26 @@ def self.matches?(crypted, *tokens) expect(User.first.username).to eq 'Noam Ben Ari' end + context 'with email' do + it 'sets passed email' do + expect do + User.create_from_provider('facebook', '123', username: 'Noam Ben Ari', email: 'bla@bla.com') { true } + end.to change { User.count }.by(1) + + expect(User.first.email).to eq 'bla@bla.com' + end + end + + context 'without email' do + it 'sets an empty string to email' do + expect do + User.create_from_provider('facebook', '123', username: 'Noam Ben Ari') { true } + end.to change { User.count }.by(1) + + expect(User.first.email).not_to be_empty + end + end + context 'with block' do it 'create user when block return true' do expect do