Skip to content
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

Hotfix twitter login #6

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion lib/generators/sorcery/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/sorcery/templates/migration/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ 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

t.timestamps :null => false
end

add_index :<%= model_class_name.tableize %>, :email, unique: true
add_index :<%= model_class_name.tableize %>, :username, unique: true

end
end
5 changes: 5 additions & 0 deletions lib/sorcery/model/submodules/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions lib/sorcery/test_helpers/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions spec/rails_app/db/migrate/core/20101224223620_create_users.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/shared_examples/user_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]') { true }
end.to change { User.count }.by(1)

expect(User.first.email).to eq '[email protected]'
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
Expand Down