Skip to content

Commit

Permalink
Adding ability to link provider to already authenticated user
Browse files Browse the repository at this point in the history
  • Loading branch information
iterion committed Feb 28, 2012
1 parent c06db83 commit 281b826
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 13 additions & 5 deletions lib/sorcery/controller/submodules/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ def login_at(provider, args = {})
end
end

# tries to login the user from provider's callback
# links provider to account if user is logged in
# otherwise it tries to login the user from provider's callback
def login_from(provider)
@provider = Config.send(provider)
@provider.process_callback(params,session)
@user_hash = @provider.get_user_hash
if user = user_class.load_from_provider(provider,@user_hash[:uid].to_s)
reset_session
auto_login(user)
user
if logged_in?
if current_user.link_from_provider(provider, @user_hash[:uid])
session[:"#{provider}_access_token"] = @user_hash[:access_token]
current_user
end
else
if user = user_class.load_from_provider(provider,@user_hash[:uid].to_s)
reset_session
auto_login(user)
user
end
end
end

Expand Down
16 changes: 15 additions & 1 deletion lib/sorcery/model/submodules/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@ def load_from_provider(provider,uid)
end

module InstanceMethods

def link_from_provider(provider,uid)
config = sorcery_config
authentication = config.authentications_class.find_by_provider_and_uid(provider, uid)
unless authentication
config.authentications_class.create!({config.authentications_user_id_attribute_name => id, config.provider_attribute_name => provider, config.provider_uid_attribute_name => uid})
else
#raise exception if we're trying to link a provider when it's linked to another account
if id == authentication.send(config.authentications_user_id_attribute_name)
self
else
#TODO make a more specific error
raise StandardError.new
end
end
end
end

end
Expand Down

0 comments on commit 281b826

Please sign in to comment.