hyperswitching

Since a few weeks I had my devise-1.2 with :omiauthable working. A few minutes ago, I completed the addition of google_apps login. While plataformatec has done an excellent job documenting facebook login. Google login missed a few things. Just add:

  def self.find_for_google_apps_oauth(access_token, signed_in_resource=nil)
    data = access_token['user_info']
    if user = User.find_by_email(data["email"])
      user
    else # Create a user with a stub password
      User.create!(:email => data["email"], :password => Devise.friendly_token[0, 20])
    end
  end

to your user model, and the following to your OmniauthCallbacksController:

  def google_apps
    # You need to implement the method below in your model
    @user = User.find_for_google_apps_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.google_apps_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
Blog comments powered by Disqus