#170 OpenID with Authlogic
Jul 13, 2009 | 11 minutes | Authentication, Plugins
Learn how to apply OpenID to an existing Authlogic setup as I show in this episode. This builds upon the app from episode 160.
- Download:
- source codeProject Files in Zip (122 KB)
- mp4Full Size H.264 Video (18.3 MB)
- m4vSmaller H.264 Video (12.4 MB)
- webmFull Size VP8 Video (31 MB)
- ogvFull Size Theora Video (24.9 MB)
Resources
- Episode 68: OpenID Authentication
- Episode 160: Authlogic
- Authlogic OpenID
- Authlogic OpenID Example
- Full Episode Source Code
bash
sudo gem install ruby-openid authlogic-oid
script/plugin install git://github.com/rails/open_id_authentication.git
rake open_id_authentication:db:create
script/generate migration add_openid_identifier_to_users openid_identifier:string
rake db:migrate
sudo gem install ruby-openid authlogic-oid script/plugin install git://github.com/rails/open_id_authentication.git rake open_id_authentication:db:create script/generate migration add_openid_identifier_to_users openid_identifier:string rake db:migrate
config/environment.rb
config.gem "authlogic-oid", :lib => "authlogic_openid"
config.gem "ruby-openid", :lib => "openid"
config.gem "authlogic-oid", :lib => "authlogic_openid" config.gem "ruby-openid", :lib => "openid"
models/user.rb
acts_as_authentic do |c|
c.openid_required_fields = [:nickname, :email]
end
private
def map_openid_registration(registration)
self.email = registration["email"] if email.blank?
self.username = registration["nickname"] if username.blank?
end
acts_as_authentic do |c| c.openid_required_fields = [:nickname, :email] end private def map_openid_registration(registration) self.email = registration["email"] if email.blank? self.username = registration["nickname"] if username.blank? end
user_sessions_controller.rb
def create
@user_session = UserSession.new(params[:user_session])
@user_session.save do |result|
if result
flash[:notice] = "Successfully logged in."
redirect_to root_url
else
render :action => 'new'
end
end
end
def create @user_session = UserSession.new(params[:user_session]) @user_session.save do |result| if result flash[:notice] = "Successfully logged in." redirect_to root_url else render :action => 'new' end end end
users_controller.rb
def create
@user = User.new(params[:user])
@user.save do |result|
if result
flash[:notice] = "Registration successful."
redirect_to root_url
else
render :action => 'new'
end
end
end
def update
@user = current_user
@user.attributes = params[:user]
@user.save do |result|
if result
flash[:notice] = "Successfully updated profile."
redirect_to root_url
else
render :action => 'edit'
end
end
end
def create @user = User.new(params[:user]) @user.save do |result| if result flash[:notice] = "Registration successful." redirect_to root_url else render :action => 'new' end end end def update @user = current_user @user.attributes = params[:user] @user.save do |result| if result flash[:notice] = "Successfully updated profile." redirect_to root_url else render :action => 'edit' end end end
users/_form.html.erb
<% if @user.openid_identifier.blank? %>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p><%= f.submit "Submit" %></p>
<h2>Or use OpenID</h2>
<% end %>
<p>
<%= f.label :openid_identifier, "OpenID URL" %><br />
<%= f.text_field :openid_identifier %>
</p>
<p><%= f.submit "Submit" %></p>
<% if @user.openid_identifier.blank? %> <p> <%= f.label :password %><br /> <%= f.password_field :password %> </p> <p> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation %> </p> <p><%= f.submit "Submit" %></p> <h2>Or use OpenID</h2> <% end %> <p> <%= f.label :openid_identifier, "OpenID URL" %><br /> <%= f.text_field :openid_identifier %> </p> <p><%= f.submit "Submit" %></p>
user_sessions/new.html.erb
<h2>Or use OpenID</h2>
<p>
<%= f.label :openid_identifier, "OpenID URL" %><br />
<%= f.text_field :openid_identifier %>
</p>
<p><%= f.submit "Submit" %></p>
<h2>Or use OpenID</h2> <p> <%= f.label :openid_identifier, "OpenID URL" %><br /> <%= f.text_field :openid_identifier %> </p> <p><%= f.submit "Submit" %></p>
css
/* embeds the openid image in the text field */
input#user_openid_identifier, input#user_session_openid_identifier {
background: url(http://openid.net/images/login-bg.gif) no-repeat;
background-color: #fff;
background-position: 0 50%;
color: #000;
padding-left: 18px;
}
/* embeds the openid image in the text field */ input#user_openid_identifier, input#user_session_openid_identifier { background: url(http://openid.net/images/login-bg.gif) no-repeat; background-color: #fff; background-position: 0 50%; color: #000; padding-left: 18px; }

