0

I'm using Devise with Google Omniauth, but I keep getting User Record Invalid error:

enter image description here

I only have this validation in my User model:

  validates :email, presence: true, uniqueness: true

User model

  devise :omniauthable, omniauth_providers: [:google_oauth2]

   def self.from_google(email:, full_name:, uid:, avatar_url:)
     create_with(uid: uid, full_name: full_name, avatar_url: avatar_url).find_or_create_by!(email: email)
   end

My routes

devise_for :users, :controllers => { omniauth_callbacks: 'users/omniauth_callbacks', :registrations => "registrations"}

omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    user = User.from_google(from_google_params)

    if user.present?
      sign_out_all_scopes
      flash[:success] = t 'devise.omniauth_callbacks.success', kind: 'Google'
      sign_in_and_redirect user, event: :authentication
    else
      flash[:alert] = t 'devise.omniauth_callbacks.failure', kind: 'Google', reason: "#{auth.info.email} is not authorized."
      redirect_to new_user_session_path
    end
  end

  protected

  def after_omniauth_failure_path_for(_scope)
    new_user_session_path
  end

  def after_sign_in_path_for(resource_or_scope)
    stored_location_for(resource_or_scope) || root_path
  end

  private

  def from_google_params
    @from_google_params ||= {
      uid: auth.uid,
      email: auth.info.email,
      full_name: auth.info.name,
      avatar_url: auth.info.image
    }
  end

  def auth
    @auth ||= request.env['omniauth.auth']
  end
end

Sessions controller

class Users::SessionsController < Devise::SessionsController
  def after_sign_out_path_for(_resource_or_scope)
    new_user_session_path
  end

  def after_sign_in_path_for(resource_or_scope)
    stored_location_for(resource_or_scope) || root_path
  end
end

Any clue on how to trace or fix this? It's driving me crazy! Thanks

Gibson
  • 2,055
  • 2
  • 23
  • 48

1 Answers1

0

Devise tries to display a relevant error message for the Spanish-translated app of your site. You are missing a translation for the Spanish error message.

Go to config/locales/es.yml and add in:

es:
  activerecord:
    errors:
      messages:
        record_invalid:
          "Spanish error message comes here"
Thomas Van Holder
  • 1,137
  • 9
  • 12