dimanche 12 juillet 2015

Rails Infinite Loop when redirect to finish_singup

I'm working on a website for a nonprofit association using rails.

And we're having a problem when the user logging in through a social network (facebook, twitter) for first time.

At the time of registration on the page through a social network, the page redirects the user to a final form (finish_signup) where the user fill it with additional data (mail, gender, date of birth) but when the user press click on Continue the pages redirects the user again to the end form (finish_singup) and thus stays in an infinite loop.

This is what the console show me when the user press Continue

http://ift.tt/1IUnxtN

When I check on the database I see that the extra information asked on the final form (finish_singup) It does not appear, only the information when the user press click on "Sing up with twitter

Only when I fill the data required (mail, gender, date of birth) manually from the database I can overcome the infinite loop and redirects the user to the main page.

this is what i have on home_controller.rb

 class HomeController < ApplicationController
  before_filter :user_has_signed_in

  def index
    logger.info request.headers['CustomHeader']
  end

  private
  def user_has_signed_in
    if user_signed_in?
      redirect_to main_path
    end
  end
end

And this is what I have on the user.rb model

class User < ActiveRecord::Base
  has_many :identities
  has_many :session_ids

  TEMP_EMAIL_PREFIX = 'change@me'
  TEMP_EMAIL_REGEX = /\Achange@me/

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :confirmable, :recoverable, :registerable, :trackable, :timeoutable, :validatable, :omniauthable, omniauth_providers: [:facebook,:twitter]

  validates_format_of :email, without: TEMP_EMAIL_REGEX, on: :update

  has_many :reports, dependent: :destroy
  has_many :affectation_votes
  has_many :comments
  has_many :likes
  has_many :activity_records

  validates :name, presence: true
  validates :email, uniqueness: true
  validates :username, uniqueness: true
  #validates :gender, presence: true
  #validates :birthdate, presence: true
  has_attached_file :avatar
  # validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/gif", "image/png", "image/bmp"] }, size: { less_than: 1.megabytes }
  do_not_validate_attachment_file_type :avatar

  def avatar_url
    omniauth_image || (avatar.url =~ /missing/ ? nil : avatar.url)
  end

  def report_count
    reports.count
  end

  def comment_count
    comments.count
  end

  def level
    number_of_reports = reports.count
    if number_of_reports >= 60
      return 9
    elsif number_of_reports >= 45
      return 8
    elsif number_of_reports >= 35
      return 7
    elsif number_of_reports >= 25
      return 6
    elsif number_of_reports >= 20
      return 5
    elsif number_of_reports >= 15
      return 4
    elsif number_of_reports >= 10
      return 3
    elsif number_of_reports >= 5
      return 2
    else
      return 1
    end
  end

  def since
    created_at.strftime('%d/%m/%Y')
  end

  def get_uid_for_provider(provider)
    if identities.count > 0
      identities.find_by_provider(provider).uid
    else
      nil
    end
  end

  def self.find_by_uid_for_provider(uid, provider)
    identity = Identity.where(provider: provider, uid: uid)
    if identity
      identity.user
    else
      nil
    end
  end

  def self.find_for_oauth(auth, signed_in_resource = nil)

    # Get the identity and user if they exist
    identity = Identity.find_for_oauth(auth)

    # If a signed_in_resource is provided, it always overrides the existing user
    # to prevent the identity beign locked with accidentally created accounts.
    # Note that this may leave zombie accounts (with no associated identity) which
    # can be cleaned up at a later date.
    user = signed_in_resource ? signed_in_resource : identity.user

    # Create the user if needed
    if user.nil?

      # Get the existing user by email if the provider gives us a verified email.
      # If no verified email was provided we assign a temporary email and ask the 
      # user to verify it on the next step via UsersController.finish_signup
      email_is_verfied = auth.info.email && (auth.info.verified || auth.info.verified_email)
      email = auth.info.email if email_is_verfied
      user = User.where( email: email ).first if email

      # Create the user if it is a new registration
      if user.nil?
          # logger.info auth
          user = User.new(
            name: auth.extra.raw_info.name,
            email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
            password: Devise.friendly_token[0,20],
            omniauth_image: auth.info.image
          )
          user.skip_confirmation!
          user.save!
        end
      end

      # Associate the identity with the user if needed
      if identity.user != user
        identity.user = user
        identity.save!
      end
      user
    end

    def email_verified?
      self.email && self.email !~ TEMP_EMAIL_REGEX
    end

This is the repo page of the server if you want take a look

http://ift.tt/1Ggy5NE

I would really appreciate if I can help in this. greetings :)

Aucun commentaire:

Enregistrer un commentaire