1

I am using authlogic and omniauth for authentication in my Rails 3.0.10 app. However when I get the callback from the omniauth provider, I am not able to create a new user session so as to sign in the user.

In other responses (and even in the authlogic docs), it says that using UserSession.create(@user, true) should be able to create and persist a new session.

However, this does not work for me. It only works if the @user has a password in the database (by inserting a password directly in the db).

But there is no password when using third-party authentication providers, hence I cannot sign in users.

Any ideas how to sign in a user without a password in authlogic?

Thanks.

Community
  • 1
  • 1
codinguser
  • 5,562
  • 2
  • 33
  • 40

1 Answers1

1

You can do something like this in your User model:

acts_as_authentic do |config|
  external = Proc.new { |r| r.externally_authenticated? }

  config.merge_validates_confirmation_of_password_field_options(:unless => external)
  config.merge_validates_length_of_password_confirmation_field_options(:unless => external)
  config.merge_validates_length_of_password_field_options(:unless => external)
end

externally_authenticated? is just a method on the user that checks what is providing the user information, and if it's one of the omniauth providers, returns true.

x1a4
  • 19,417
  • 5
  • 40
  • 40
  • How are you checking for whether or not the user is authenticated via omniauth or not? i.e. what are you using for `externally_authenticated?` – x1a4 May 19 '12 at 08:00
  • It did not work for me to declare external in the config and pass it as an option. I had to pass in `externally_authenticated?` itself as the option for it to work. The method implementation is a one-liner `!authentications.empty? or password.blank?` – codinguser May 21 '12 at 14:03