diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..cf3fb0c --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..42bc789 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 4487569..0936014 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c406b5e..4e94dec 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -43,67 +43,80 @@ shadow: shadow }); var infoWindow = new google.maps.InfoWindow({ - content: contentString + 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'); }); }); } diff --git a/app/views/main/_combo_form.html.erb b/app/views/main/_combo_form.html.erb index d3b7ad5..56cd5ba 100644 --- a/app/views/main/_combo_form.html.erb +++ b/app/views/main/_combo_form.html.erb @@ -1,36 +1,36 @@

Adopt this Hydrant

-<%= form_tag "#", :method => "GET", :class => "combo_form" do %> -
- <%= 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" %>
- <%= label_tag "account_existing", radio_button_tag("account", "existing").html_safe + "I already signed up" %>
+<%= form_for :user, :html => {:class => "combo_form"} do |f| %> +
+ <%= 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" %>
+ <%= f.label "existing", radio_button_tag("user", "existing").html_safe + "I already signed up" %>
-