Skip to content

Add build_from method #54

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

Merged
merged 9 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## HEAD

* Add facebook user_info_path option to initializer.rb [#63](https://github.com/Sorcery/sorcery/pull/63)
* Add new function: `build_from` (allows building a user instance from OAuth without saving) [#54](https://github.com/Sorcery/sorcery/pull/54)

## 0.11.0

Expand Down
9 changes: 9 additions & 0 deletions lib/sorcery/controller/submodules/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ def create_from(provider_name, &block)
@user = user_class.create_from_provider(provider_name, @user_hash[:uid], attrs, &block)
end

# follows the same patterns as create_from, but builds the user instead of creating
def build_from(provider_name, &block)
sorcery_fetch_user_hash provider_name
config = user_class.sorcery_config

attrs = user_attrs(@provider.user_info_mapping, @user_hash)
@user = user_class.build_from_provider(attrs, &block)
end

def user_attrs(user_info_mapping, user_hash)
attrs = {}
user_info_mapping.each do |k, v|
Expand Down
15 changes: 15 additions & 0 deletions lib/sorcery/model/submodules/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ def create_from_provider(provider, uid, attrs)
end
user
end

# NOTE: Should this build the authentication as well and return [user, auth]?
# Currently, users call this function for the user and call add_provider_to_user after saving
def build_from_provider(attrs)
user = new
attrs.each do |k, v|
user.send(:"#{k}=", v)
end

if block_given?
return false unless yield user
end

user
end
end

module InstanceMethods
Expand Down