#304 OmniAuth Identity
With the release of OmniAuth 1.0 there is a new Identity strategy which allows users to register/login with a password if they don't want to use an external provider.
- Download:
- source codeProject Files in Zip (112 KB)
- mp4Full Size H.264 Video (24.3 MB)
- m4vSmaller H.264 Video (13.1 MB)
- webmFull Size VP8 Video (15.3 MB)
- ogvFull Size Theora Video (27.2 MB)
Resources
bash
rails g model identity name:string email:string password_digest:string rake db:migrate rails g controller identities
Gemfile
gem 'omniauth-identity'
config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do # ... provider :identity, on_failed_registration: lambda { |env| IdentitiesController.action(:new).call(env) } end
models/identity.rb
class Identity < OmniAuth::Identity::Models::ActiveRecord validates_presence_of :name validates_uniqueness_of :email validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i end
sessions/new.html.erb
<p> <strong>Don't use these services?</strong> <%= link_to "Create an account", new_identity_path %> or login below. </p> <%= form_tag "/auth/identity/callback" do %> <div class="field"> <%= label_tag :auth_key, "Email" %><br> <%= text_field_tag :auth_key %> </div> <div class="field"> <%= label_tag :password %><br> <%= password_field_tag :password %> </div> <div class="actions"><%= submit_tag "Login" %></div> <% end %>
routes.rb
resources :identities
identities_controller.rb
def new @identity = env['omniauth.identity'] end
identities/new.html.erb
<%= form_tag "/auth/identity/register" do %> <% if @identity && @identity.errors.any? %> <div class="error_messages"> <h2><%= pluralize(@identity.errors.count, "error") %> prohibited this account from being saved:</h2> <ul> <% @identity.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= label_tag :name %><br> <%= text_field_tag :name, @identity.try(:name) %> </div> <div class="field"> <%= label_tag :email %><br> <%= text_field_tag :email, @identity.try(:email) %> </div> <div class="field"> <%= label_tag :password %><br> <%= password_field_tag :password %> </div> <div class="field"> <%= label_tag :password_confirmation %><br> <%= password_field_tag :password_confirmation %> </div> <div class="actions"><%= submit_tag "Register" %></div> <% end %>