몇 가지 요청이이 질문에 대한 답변으로 끝났으므로 Andy Mejia가 결국이 질문에 대한 답을 얻었습니다.
def callback
provider = params[:provider]
old_session = session.clone # The session gets reset when we login, so let's backup the data we need
begin
if @user = login_from(provider) # User had already logged in through Facebook before
restore_session(old_session) # Cleared during login
else
# If there's already an user with this email, just hook this Facebook account into it.
@user = UserAccount.with_insensitive_email(get_facebook_hash[:user_info][:email]).first
# If there's no existing user, let's create a new account from scratch.
@user ||= create_from(provider) # Be careful, validation is turned off because Sorcery is a bitch!
login_without_authentication(@user)
end
@user.update_attributes_from_facebook!(get_facebook_hash)
rescue ::OAuth2::Error => e
p e
puts e.message
puts e.backtrace
redirect_to after_login_url_for(@user), :alert => "Failed to login from #{provider.titleize}!"
return
end
redirect_to after_login_url_for(@user)
end
나는 희망이 솔루션은 다른 사람에게 도움이 될 것입니다 : 나는 또한 우리의 컨트롤러 로직을 포함하고 맥락에서이 코드를 표시하려면
# Returns the hash that contains the information that was passed back from Facebook.
# It only makes sense to call this method on the callback action.
#
# Example hash:
# {:user_info=>{:id=>"562515238", :name=>"Andrés Mejía-Posada", :first_name=>"Andrés", :last_name=>"Mejía-Posada", :link=>"http://www.facebook.com/andmej", :username=>"andmej", :gender=>"male", :email=>"[email protected]", :timezone=>-5, :locale=>"en_US", :verified=>true, :updated_time=>"2011-12-31T21:39:24+0000"}, :uid=>"562515238"}
def get_facebook_hash
provider = Rails.application.config.sorcery.facebook
access_token = provider.process_callback(params, session)
hash = provider.get_user_hash
hash.merge!(:access_token => access_token.token)
hash.each { |k, v| v.symbolize_keys! if v.is_a?(Hash) }
end
# Method added to the User Account model class
def update_attributes_from_facebook!(facebook_hash)
self.first_name = facebook_hash[:user_info][:first_name] if self.first_name.blank?
self.last_name = facebook_hash[:user_info][:last_name] if self.last_name.blank?
self.facebook_access_token = facebook_hash[:access_token]
self.email ||= facebook_hash[:user_info][:email]
unless facebook_authentication?
authentications.create!(:provider => "facebook", :uid => facebook_hash[:uid])
end
self.build_facebook_profile if facebook_profile.blank?
save!
self.facebook_profile.delay.fetch_from_facebook! # Get API data
end
: 우리는 다음과 같은 기능을 적응 마법 내에서 소스를 사용 .
이것은 버그는 아니지만 찾고있는 기능 (기존 사용자에게 인증 추가)은 현재 Sorcery에서 지원하지 않는 것 같습니다. 그걸 앱에 적용하는 방법의 예가있는 사람의 이야기를 듣고 싶습니다. – Andrew
@Dan 행운이 있나요? –
@ChristianFazzini 우리는 운이 좋았습니다. 이 스레드에 우리의 접근 방식을 게시했습니다. –