Created
May 29, 2011 11:47
-
-
Save akonan/997682 to your computer and use it in GitHub Desktop.
Steps to get Google OpenID working with Devise and Omniauth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ Add link to sign in with Google to your layout / views: | |
= link_to "Sign in with Google", user_omniauth_authorize_path(:open_id, :openid_url => "https://www.google.com/accounts/o8/id") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To the Devise initializer (initializer/devise.rb) before setup block: | |
require "openid/store/filesystem" | |
# and inside setup block: | |
config.omniauth :open_id, OpenID::Store::Filesystem.new("/tmp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add this line to your Gemfile | |
gem "oa-openid", :require => "omniauth/openid" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# And create app/controllers/users/omniauth_callbacks_controller.rb: | |
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def open_id | |
# You need to implement the method below in your model | |
@user = User.find_for_open_id(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.open:id_data"] = env["openid.ext1"] | |
redirect_to new_user_registration_url | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# routes file needs to have this: | |
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add to inside your user model | |
devise :omniauthable | |
def self.find_for_open_id(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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not seem to be current. I've run nearly the same thing and get:
Could not find omniauth provider :open_id
from this line:
link_to I18n.t('session.sign_in'), user_omniauth_authorize_path(:open_id, :openid_url => "https://www.google.com/accounts/o8/id"), options
So mine is a bit more complicated but it doesn't matter, since :open_id is missing. I'll post an update if/ever i fix it