Wire up sign-up form

This commit is contained in:
Erik Michaels-Ober 2011-03-06 10:28:00 -08:00
parent d3dd6fbac3
commit 0da5ed46cd
7 changed files with 132 additions and 82 deletions

View File

@ -0,0 +1,15 @@
class SessionsController < Devise::SessionsController
respond_to :json
def create
resource = warden.authenticate(:scope => resource_name)
sign_in(resource_name, resource)
respond_with resource
end
def destroy
signed_in = signed_in?(resource_name)
sign_out(resource_name) if signed_in
respond_with signed_in
end
end

View File

@ -0,0 +1,18 @@
class UsersController < Devise::RegistrationsController
respond_to :json
def create
build_resource
if resource.save
if resource.active?
sign_in(resource_name, resource)
else
expire_session_data_after_sign_in!
end
else
clean_up_passwords(resource)
end
respond_with resource
end
end

View File

@ -1,5 +1,5 @@
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :email, :name, :organization, :voice_number, :sms_number, :password, :password_confirmation, :remember_me
has_many :hydrants
end

View File

@ -46,64 +46,77 @@
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
if(openInfoWindow) {
if(openInfoWindow != undefined) {
openInfoWindow.close();
}
infoWindow.open(map, marker);
$('.combo_form').data('state', 'new');
$('.combo_form').data('state', 'user_new');
$('.combo_form').submit(function() {
var errors = []
if(!/[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/.test($('.email').val())) {
errors.push($('.email'));
$('.email_label').addClass('error', 500);
$('.email').addClass('error', 500);
if(!/[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/.test($('#user_email').val())) {
errors.push($('#user_email'));
$('#user_email_label').addClass('error', 500);
$('#user_email').addClass('error', 500);
} else {
$('.email_label').removeClass('error', 500);
$('.email').removeClass('error', 500);
$('#user_email_label').removeClass('error', 500);
$('#user_email').removeClass('error', 500);
}
if($(this).data('state') === 'new') {
if($('.name').val() === '') {
errors.push($('.name'));
$('.name_label').addClass('error', 500);
$('.name').addClass('error', 500);
if($(this).data('state') === 'user_new') {
if($('#user_name').val() === '') {
errors.push($('#user_name'));
$('#user_name_label').addClass('error', 500);
$('#user_name').addClass('error', 500);
} else {
$('.name_label').removeClass('error', 500);
$('.name').removeClass('error', 500);
$('#user_name_label').removeClass('error', 500);
$('#user_name').removeClass('error', 500);
}
if($('.choose_password').val() === '') {
errors.push($('.choose_password'));
$('.choose_password_label').addClass('error', 500);
$('.choose_password').addClass('error', 500);
if($('#user_password_confirmation').val().length < 6 || $('#user_password_confirmation').val().length > 20) {
errors.push($('#user_password_confirmation'));
$('#user_password_confirmation_label').addClass('error', 500);
$('#user_password_confirmation').addClass('error', 500);
} else {
$('.choose_password_label').removeClass('error', 500);
$('.choose_password').removeClass('error', 500);
$('#user_password_confirmation_label').removeClass('error', 500);
$('#user_password_confirmation').removeClass('error', 500);
}
if(errors.length > 0) {
errors[0].focus();
} else {
$.post('/sign_up.json', $(this).serialize(), function(data) {
$.post('<%= user_registration_path :format => "json" %>', {
'remote' : true,
'commit' : $('#user_new_submit').val(),
'utf8' : '✓',
'user' : {
'email' : $('#user_email').val(),
'name' : $('#user_name').val(),
'organization' : $('#user_organization').val(),
'voice_number' : $('#user_voice_number').val(),
'sms_number' : $('#user_sms_number').val(),
'password' : $('#user_password_confirmation').val(),
'password_confirmation' : $('#user_password_confirmation').val()
}
}, function(data) {
});
}
} else if($(this).data('state') === 'existing') {
if($('.password').val() === '') {
errors.push($('.password'));
$('.password_label').addClass('error', 500);
$('.password').addClass('error', 500);
} else if($(this).data('state') === 'user_existing') {
if($('#user_password').val() === '') {
errors.push($('#user_password'));
$('#user_password_label').addClass('error', 500);
$('#user_password').addClass('error', 500);
} else {
$('.password_label').removeClass('error', 500);
$('.password').removeClass('error', 500);
$('#user_password_label').removeClass('error', 500);
$('#user_password').removeClass('error', 500);
}
if(errors.length > 0) {
errors[0].focus();
} else {
$.post('/sign_in.json', $(this).serialize(), function(data) {
$.post('<%= user_session_path :format => "json" %>', $(this).serialize(), function(data) {
});
}
} else if($(this).data('state') === 'forgot_password') {
} else if($(this).data('state') === 'user_forgot_password') {
if(errors.length > 0) {
errors[0].focus();
} else {
$.post('/forgot_password.json', $(this).serialize(), function(data) {
$.post('<%= user_password_path :format => "json" %>', $(this).serialize(), function(data) {
});
}
}
@ -130,27 +143,27 @@
return false;
}
});
$('.box input[type="radio"]').live('click', function() {
$('.combo_form input[type="radio"]').live('click', function() {
var self = $(this);
if('new' == self.val()) {
$('.forgot_password_fields').slideUp();
$('.account_existing_fields').slideUp();
$('.account_new_fields').slideDown();
$('.combo_form').data('state', 'new');
$('#user_forgot_password_fields').slideUp();
$('#user_existing_fields').slideUp();
$('#user_new_fields').slideDown();
$('.combo_form').data('state', 'user_new');
} else if('existing' == self.val()) {
$('.account_new_fields').slideUp();
$('.account_existing_fields').slideDown(function() {
$('.combo_form').data('state', 'existing');
$('.forgot_password').click(function() {
$('.account_existing_fields').slideUp();
$('.forgot_password_fields').slideDown(function() {
$('.remembered_password').click(function() {
$('.forgot_password_fields').slideUp();
$('.account_existing_fields').slideDown();
$('.combo_form').data('state', 'existing');
$('#user_new_fields').slideUp();
$('#user_existing_fields').slideDown(function() {
$('.combo_form').data('state', 'user_existing');
$('#user_forgot_password_link').click(function() {
$('#user_existing_fields').slideUp();
$('#user_forgot_password_fields').slideDown(function() {
$('#user_remembered_password').click(function() {
$('#user_forgot_password_fields').slideUp();
$('#user_existing_fields').slideDown();
$('.combo_form').data('state', 'user_existing');
});
});
$('.combo_form').data('state', 'forgot_password');
$('.combo_form').data('state', 'user_forgot_password');
});
});
}

View File

@ -1,36 +1,36 @@
<div class="box">
<h2>Adopt this Hydrant</h2>
<%= form_tag "#", :method => "GET", :class => "combo_form" do %>
<div class="common_fields">
<%= label_tag "email", "Email address", :class => "email_label" %>
<%= text_field_tag "email", nil, :tabindex => 1, :class => "email" %>
<%= label_tag "account_new" , radio_button_tag("account", "new", true, :tabindex => 2).html_safe + "I haven\\'t signed up yet" %><br />
<%= label_tag "account_existing", radio_button_tag("account", "existing").html_safe + "I already signed up" %><br />
<%= form_for :user, :html => {:class => "combo_form"} do |f| %>
<div id="common_fields">
<%= f.label "email", "Email address", :id => "user_email_label" %>
<%= f.text_field "email", :tabindex => 1 %>
<%= f.label "new" , radio_button_tag("user", "new", true, :tabindex => 2).html_safe + "I haven\\'t signed up yet" %><br />
<%= f.label "existing", radio_button_tag("user", "existing").html_safe + "I already signed up" %><br />
</div>
<div class="account_new_fields">
<%= label_tag "name", "Name", :class => "name_label" %>
<%= text_field_tag "name", nil, :tabindex => 3, :class => "name" %>
<%= label_tag "organization", "Organization", :class => "organization_label" %>
<%= text_field_tag "organization", nil, :tabindex => 4, :class => "organization" %>
<%= label_tag "voice_number", "Phone number", :class => "voice_number_label" %>
<%= text_field_tag "voice_number", nil, :tabindex => 5, :class => "voice_number" %>
<%= label_tag "sms_number", "SMS number", :class => "sms_number_label" %>
<%= text_field_tag "sms_number", nil, :tabindex => 6, :class => "sms_number" %>
<%= label_tag "choose_password", "Choose a password", :class => "choose_password_label" %>
<%= password_field_tag "choose_password", nil, :tabindex => 7, :class => "choose_password" %>
<%= submit_tag "Sign up", :tabindex => 8 %>
<div id="user_new_fields">
<%= f.label "name", "Name", :id => "user_name_label" %>
<%= f.text_field "name", :tabindex => 3 %>
<%= f.label "organization", "Organization", :id => "user_organization_label" %>
<%= f.text_field "organization", :tabindex => 4 %>
<%= f.label "voice_number", "Phone number", :id => "user_voice_number_label" %>
<%= f.text_field "voice_number", :tabindex => 5 %>
<%= f.label "sms_number", "SMS number", :id => "user_sms_number_label" %>
<%= f.text_field "sms_number", :tabindex => 6 %>
<%= f.label "password_confirmation", "Choose a password", :id => "user_password_confirmation_label" %>
<%= f.password_field "password_confirmation", :tabindex => 7 %>
<%= f.submit "Sign up", :tabindex => 8, :id => "user_new_submit" %>
<p>By signing up you accept the <%= link_to "Terms of Service", "#", :tabindex => 9 %>.</p>
</div>
<div class="account_existing_fields" style="display: none;">
<%= label_tag "password", "Password", :class => "password_label" %>
<%= password_field_tag "password", nil, :tabindex => 0, :class => "password" %>
<%= label_tag "remember_me", (check_box_tag("remember_me", nil, true).html_safe + "Stay signed in"), :class => "remember_me_label" %><br />
<%= submit_tag "Sign in", :tabindex => 0 %>
<p><%= link_to "Forgot your password?", "#", :class => "forgot_password", :tabindex => 0 %></p>
<div id="user_existing_fields" style="display: none;">
<%= f.label "password", "Password", :id => "user_password_label" %>
<%= f.password_field "password", :tabindex => 0 %>
<%= f.label "remember_me", (f.check_box("remember_me").html_safe + "Stay signed in"), :id => "user_remember_me_label" %><br />
<%= f.submit "Sign in", :tabindex => 0, :id => "user_existing_submit" %>
<p><%= link_to "Forgot your password?", "#", :id => "user_forgot_password_link", :tabindex => 0 %></p>
</div>
<div class="forgot_password_fields" style="display: none;">
<%= submit_tag "Email me my password", :tabindex => 0 %>
<p><%= link_to "Never mind. I remembered my password.", "#", :class => "remembered_password", :tabindex => 0 %></p>
<div id="user_forgot_password_fields" style="display: none;">
<%= f.submit "Email me my password", :tabindex => 0, :id => "user_forgot_password_submit" %>
<p><%= link_to "Never mind. I remembered my password.", "#", :id => "user_remembered_password", :tabindex => 0 %></p>
</div>
<% end %>
</div>

View File

@ -26,10 +26,10 @@ Devise.setup do |config|
# config.params_authenticatable = true
# Tell if authentication through HTTP Basic Auth is enabled. False by default.
# config.http_authenticatable = false
config.http_authenticatable = false
# Set this to true to use Basic Auth for AJAX requests. True by default.
# config.http_authenticatable_on_xhr = true
config.http_authenticatable_on_xhr = false
# The realm used in Http Basic Authentication
# config.http_authentication_realm = "Application"
@ -124,7 +124,7 @@ Devise.setup do |config|
# access, but formats like :xml or :json, should return 401.
# If you have any extra navigational formats, like :iphone or :mobile, you
# should add them to the navigational formats lists. Default is [:html]
# config.navigational_formats = [:html, :iphone]
config.navigational_formats = [:html, :json]
# ==> Warden configuration
# If you want to use other strategies, that are not (yet) supported by Devise,

View File

@ -1,4 +1,8 @@
AdoptAHydrant::Application.routes.draw do
devise_for :users
root :to => "main#index"
devise_for :users, :controllers => {
:registrations => 'users',
:sessions => 'sessions'
}
resource :user
root :to => 'main#index'
end