Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8596965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:48:21+00:00 2026-06-12T00:48:21+00:00

The example sorcery code shown on github appears to me to create duplicate accounts

  • 0

The example sorcery code shown on github appears to me to create duplicate accounts if it is extended to allow for multiple sign in methods (which is the whole point of oauth). You can see in the snipit here that create_from() will be called if login_from() does not succeed.

GITHUB AT at https://github.com/NoamB/sorcery-example-app/blob/master/app/controllers/oauths_controller.rb

def callback
provider = params[:provider]
begin
if @user = login_from(provider)
  redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
else
  begin
    @user = create_from(provider)

Investigating the source code for create_from in all cases a new User Account record will be created. This would not be correct, if a User account record already exists.

My question: What sorcery methods should be called on the first facebook connect, if a User account has been created by some means other than facebook. login_from will fail, and create_from will generate a duplicate usser record?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:48:23+00:00Added an answer on June 12, 2026 at 12:48 am

    Several requests have come through for an answer to this question, so I am providing the answer that Andy Mejia part of my team eventually arrived at for this question. We used the source within sorcery to adapt the following functions:

    # 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=>"andmej@gmail.com", :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
    

    To show these code in context, I am also including logic from our controller:

    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
    

    I hope this solution is helpful to others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorcery authentication gem: https://github.com/NoamB/sorcery Sorcery's creator provides an example Rails app with Sorcery test
Example CSS #wrap{margin:20px} Code prettify wraps the whole line in .com <span class=com>#wrap{margin:20px}</span> Somebody
Example I have this line of code in PHP, and I want to pass
Example Outlook: its only one process but can have multiple windows (user can double
Example code: int hour = 0; bool saveData = true; if(hour > 0) doSomeMethod();
Example in coffeescript window.APP = Ember.Application.create() APP.stuff = Ember.Object.create( name: The Name ) APP.things
Example Code: EmigProb<-c(rep(seq(0.1,0.8,length=5),4),rep(seq(0.1,0.8,length=5),4)) RemainEmigProb<-c(rep(0.2,5),rep(0.4,5),rep(0.6,5),rep(0.8,5),rep(0.2,5),rep(0.4,5),rep(0.6,5),rep(0.8,5)) Value<-rnorm(40,5,3) Parameter<-c(rep(Survival,20),rep(Resight,20)) fakedata<-data.frame(EmigProb=EmigProb,RemainEmigProb=RemainEmigProb,Value=Value,Parameter=Parameter) q <-ggplot(fakedata,aes(EmigProb,Value,shape=factor(RemainEmigProb),colour=factor(Parameter),linetype=factor(RemainEmigProb)))+scale_colour_discrete(Parameter)+scale_linetype_discrete(Remain Emigrant Probability)+scale_shape_manual(Remain Emigrant Probability,values=c(0,5,6,15)) q
Example: Class *_obj1; Class *_obj2; void doThis(Class *obj) {} void create() { Class *obj1
Example Code on JSFiddle Hi, I would like to access the child nodes (squares)
example: i need the numbers of which the sum of the amount where id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.