Search code examples
ruby-on-railsfacebookmodelcontrolleromniauth

undefined method `persisted?' for true:TrueClass


Im getting this error and im not sure why. There are similar problems like this that couldnt solve my case.

  • could be from user = User.where(:email => auth.info.email).first
  • could be from where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  • or even on the persisted? part in the omniauth_controller

What could be the possible options or solutions to this error?? thank you!! ^^

Omniauth_controller: where persisted? error takes place at.

def facebook

    puts "1111111111 yayayay"
    # raise request.env["omniauth.params"].inspect

        user = User.from_omniauth((request.env["omniauth.auth"]), (request.env["omniauth.params"]), (request.remote_ip))

        if user.persisted?
      puts "3333333 okayay"
            sign_in_and_redirect @user, :event => :authentication
            set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
        else
      puts "wtf wtf wtf wtf wtf"
            session["devise.facebook_data"] = request.env["omniauth.auth"]
            redirect_to new_user_registration_url

        end
    end

User Model where i create stripe verification and facebook omniauth.

#gathering the info from social media when making an account.
def self.from_omniauth(auth, auth_params, request)

 anonymous_username = "NewUser#{User.last.id + 100}"
 generated_password = Devise.friendly_token[0,20]

 user = User.where(:email => auth.info.email).first

 puts "auth params are #{auth_params}"
 puts "user type is #{auth_params["user_type"]}"



 if user
  return user
else
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.confirmed_at = Time.now
    user.fullname = auth.info.name
    user.provider = auth.provider
    user.user_type = auth_params["user_type"] 
    user.uid = auth.uid
    user.username = anonymous_username
    user.email = auth.info.email
    if auth.info.image.present?
     avatar_url = process_uri(auth.info.image)
     user.avatar = URI.parse(avatar_url)
   end 
   user.password = generated_password 
   user.save
   user
 end

#reviser account creation
user = User.where(:email => auth.info.email).first
puts "before the if #{user}  and #{user.user_type}"
if user.user_type == 'reviser'
  puts 'this is reviser starting to create account'
require "stripe"
  email = auth.info.email

  begin

  Stripe.api_key = ENV['STRIPE_SECRET_KEY']

    account = Stripe::Account.create(
    {
      :country => 'US',
      :managed => true,
      :tos_acceptance => {
        :date => Time.now.to_i,
        :ip => request
        },
      :transfer_schedule => {
        :interval => 'manual'
        },
        :email => email,

        :legal_entity => {
          :type => 'individual'
          }

        }
        )

    verification = Verification.create!(
        user_id: user.id,
        country: 'US',
        email: email,
        terms_of_service: true


        )


        puts "success!1: #{account.id}"
        puts "success!4: #{account.keys.secret}"
        puts "success!5: #{account.keys.publishable}"
        verification.update_attributes account_id: account.id





      rescue Stripe::InvalidRequestError => e
        if e.message == "An account with this email already exists."
         redirect_to stripe_path, alert: "An account with this email already exists."
       else
        puts "#{e.message}"
        flash[:error]= e.message
      end

    rescue Stripe::AuthenticationError => e
      redirect_to stripe_path, alert: "oops! Please let us know about this error! Please Try Again Later!"
  # Authentication with Stripe's API failed
  # (maybe you changed API keys recently)
rescue Stripe::APIConnectionError => e
  redirect_to stripe_path, alert: "oops! Something went wrong! Please Try Again!"
  # Network communication with Stripe failed
rescue Stripe::StripeError => e
  redirect_to stripe_path, alert: "oops! Something went wrong! Please Try Again!"
  # Display a very generic error to the user, and maybe send
  # yourself an email

end
end



end
end

Solution

  • Your method from_omniauth is returning true because of the line user.save. Return a user at the end like:

    def self.from_omniauth(auth, auth_params, request)
    
      anonymous_username = "NewUser#{User.last.id + 100}"
      generated_password = Devise.friendly_token[0,20]
    
      user = User.where(:email => auth.info.email).first
    
      puts "auth params are #{auth_params}"
      puts "user type is #{auth_params["user_type"]}"
    
    
    
      if user
        return user
      else
        where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
          user.confirmed_at = Time.now
          user.fullname = auth.info.name
          user.provider = auth.provider
          user.user_type = auth_params["user_type"]
          user.uid = auth.uid
          user.username = anonymous_username
          user.email = auth.info.email
          if auth.info.image.present?
            avatar_url = process_uri(auth.info.image)
            user.avatar = URI.parse(avatar_url)
          end
          user.password = generated_password
          user.save
        end
    
    #reviser account creation
        user = User.where(:email => auth.info.email).first
        puts "before the if #{user}  and #{user.user_type}"
        if user.user_type == 'reviser'
          puts 'this is reviser starting to create account'
          require "stripe"
          email = auth.info.email
    
          begin
    
            Stripe.api_key = ENV['STRIPE_SECRET_KEY']
    
            account = Stripe::Account.create(
                {
                    :country => 'US',
                    :managed => true,
                    :tos_acceptance => {
                        :date => Time.now.to_i,
                        :ip => request
                    },
                    :transfer_schedule => {
                        :interval => 'manual'
                    },
                    :email => email,
    
                    :legal_entity => {
                        :type => 'individual'
                    }
    
                }
            )
    
            verification = Verification.create!(
                user_id: user.id,
                country: 'US',
                email: email,
                terms_of_service: true
    
    
            )
    
    
            puts "success!1: #{account.id}"
            puts "success!4: #{account.keys.secret}"
            puts "success!5: #{account.keys.publishable}"
            verification.update_attributes account_id: account.id
    
    
    
    
    
          rescue Stripe::InvalidRequestError => e
            if e.message == "An account with this email already exists."
              redirect_to stripe_path, alert: "An account with this email already exists."
            else
              puts "#{e.message}"
              flash[:error]= e.message
            end
    
          rescue Stripe::AuthenticationError => e
            redirect_to stripe_path, alert: "oops! Please let us know about this error! Please Try Again Later!"
              # Authentication with Stripe's API failed
              # (maybe you changed API keys recently)
          rescue Stripe::APIConnectionError => e
            redirect_to stripe_path, alert: "oops! Something went wrong! Please Try Again!"
              # Network communication with Stripe failed
          rescue Stripe::StripeError => e
            redirect_to stripe_path, alert: "oops! Something went wrong! Please Try Again!"
            # Display a very generic error to the user, and maybe send
            # yourself an email
    
          end
        end
    
        user
    
      end
    end