From d55d57487479d3f3242594e1d698ec9625cb763e Mon Sep 17 00:00:00 2001 From: rt00492 <rt00492@surrey.ac.uk> Date: Sun, 24 Apr 2022 23:16:51 +0100 Subject: [PATCH] Removing Devise and Refactoring Sign Up page --- app/assets/stylesheets/application.scss | 5 ++ app/controllers/application_controller.rb | 23 +++++--- app/controllers/home_controller.rb | 2 +- app/controllers/registrations_controller.rb | 19 +++++++ app/helpers/registrations_helper.rb | 2 + app/models/sign_up_validation.rb | 15 ++++++ app/vaildators/password_validator.rb | 12 +++++ .../{devise => }/confirmations/new.html.erb | 0 app/views/devise/registrations/new.html.erb | 54 ------------------- app/views/home/home.html.erb | 2 +- .../mailer/confirmation_instructions.html.erb | 0 .../mailer/email_changed.html.erb | 0 .../mailer/password_change.html.erb | 0 .../reset_password_instructions.html.erb | 0 .../mailer/unlock_instructions.html.erb | 0 .../{devise => }/passwords/edit.html.erb | 0 app/views/{devise => }/passwords/new.html.erb | 0 .../{devise => }/registrations/edit.html.erb | 0 app/views/registrations/new.html.erb | 47 ++++++++++++++++ app/views/{devise => }/sessions/new.html.erb | 0 .../shared/_error_messages.html.erb | 0 app/views/{devise => }/shared/_links.html.erb | 0 app/views/{devise => }/unlocks/new.html.erb | 0 config/application.rb | 4 ++ config/routes.rb | 4 +- db/migrate/20220403154425_create_users.rb | 5 +- db/schema.rb | 4 +- test/MessagesTest.rb | 5 ++ .../registrations_controller_test.rb | 7 +++ 29 files changed, 140 insertions(+), 70 deletions(-) create mode 100644 app/controllers/registrations_controller.rb create mode 100644 app/helpers/registrations_helper.rb create mode 100644 app/models/sign_up_validation.rb create mode 100644 app/vaildators/password_validator.rb rename app/views/{devise => }/confirmations/new.html.erb (100%) delete mode 100644 app/views/devise/registrations/new.html.erb rename app/views/{devise => }/mailer/confirmation_instructions.html.erb (100%) rename app/views/{devise => }/mailer/email_changed.html.erb (100%) rename app/views/{devise => }/mailer/password_change.html.erb (100%) rename app/views/{devise => }/mailer/reset_password_instructions.html.erb (100%) rename app/views/{devise => }/mailer/unlock_instructions.html.erb (100%) rename app/views/{devise => }/passwords/edit.html.erb (100%) rename app/views/{devise => }/passwords/new.html.erb (100%) rename app/views/{devise => }/registrations/edit.html.erb (100%) create mode 100644 app/views/registrations/new.html.erb rename app/views/{devise => }/sessions/new.html.erb (100%) rename app/views/{devise => }/shared/_error_messages.html.erb (100%) rename app/views/{devise => }/shared/_links.html.erb (100%) rename app/views/{devise => }/unlocks/new.html.erb (100%) create mode 100644 test/MessagesTest.rb create mode 100644 test/controllers/registrations_controller_test.rb diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index b4bdcd8..dd6cc5d 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -28,3 +28,8 @@ .background { background-color:aquamarine; } + +.error_message { + font-size: 1rem; + font-weight: 300; +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 630ac35..2dcee40 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -16,7 +16,7 @@ class ApplicationController < ActionController::Base session[:jwt_token] = login.header['authorization'] session[:logged_in] = true - + render "home" end @@ -32,7 +32,7 @@ class ApplicationController < ActionController::Base render "home" end - def sign_up_test(sign_up_params) + def sign_up_http(sign_up_params) sign_up = HTTParty.post('http://172.17.0.1:3001/api/signup', :body => { :user => { :email => sign_up_params['email'], @@ -40,12 +40,23 @@ class ApplicationController < ActionController::Base }}.to_json, :headers => { 'Content-Type' => 'application/json'}) - session[:user_id] = sign_up.body['id'] - session[:jwt_token] = sign_up.header['authorization'] - session[:logged_in] = true puts sign_up + if sign_up['errors'] != nil + session[:http_errors] = sign_up['errors'] + redirect_to sign_up_path + else + session[:user_id] = sign_up['data']['id'] + session[:jwt_token] = sign_up.header['authorization'] + session[:logged_in] = true - render "home" + user = User.new(:auth_id => session[:user_id]) + user.save! + puts "kill" + redirect_to root_path + end + + + end def log_out diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 43d513c..bed6ba7 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -17,7 +17,7 @@ class HomeController < ApplicationController end def test_sign_up - sign_up_params = {"email" => "test2@surrey.ac.uk", "password" => "1234567"} + sign_up_params = {"email" => "test3@surrey.ac.uk", "password" => "1234567"} sign_up_test(sign_up_params) end diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb new file mode 100644 index 0000000..65f9eae --- /dev/null +++ b/app/controllers/registrations_controller.rb @@ -0,0 +1,19 @@ +class RegistrationsController < ApplicationController + def new + @sign_up_validation = SignUpValidation.new + + end + + + def sign_up + @sign_up_validation = SignUpValidation.new(params["sign_up_validation"].permit(:email, :password, :password_confirmation, :authenticity_token, :commit)) + if @sign_up_validation.valid? + puts "valid" + sign_up_params = {"email" => @sign_up_validation.email, "password" => @sign_up_validation.password} + sign_up_http(sign_up_params) + else + puts @sign_up_validation.errors.full_messages + render 'new', status: :unprocessable_entity + end + end +end diff --git a/app/helpers/registrations_helper.rb b/app/helpers/registrations_helper.rb new file mode 100644 index 0000000..b100376 --- /dev/null +++ b/app/helpers/registrations_helper.rb @@ -0,0 +1,2 @@ +module RegistrationsHelper +end diff --git a/app/models/sign_up_validation.rb b/app/models/sign_up_validation.rb new file mode 100644 index 0000000..10f41dc --- /dev/null +++ b/app/models/sign_up_validation.rb @@ -0,0 +1,15 @@ +class SignUpValidation + include ActiveModel::Model + include ActiveModel::Validations + + attr_accessor :email, :password, :password_confirmation, :authenticity_token, :commit + + + validates :email, :password, :password_confirmation, presence: true + validates :email, format: { with: URI::MailTo::EMAIL_REGEXP } + validates :password, password: true + validates_confirmation_of :password + + + +end \ No newline at end of file diff --git a/app/vaildators/password_validator.rb b/app/vaildators/password_validator.rb new file mode 100644 index 0000000..4b6cbb7 --- /dev/null +++ b/app/vaildators/password_validator.rb @@ -0,0 +1,12 @@ +class PasswordValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + if to_short(value) + record.errors.add(attribute, "needs to be 8 characters or more") + end + end + + def to_short(value) + value.length < 8 + end +end + diff --git a/app/views/devise/confirmations/new.html.erb b/app/views/confirmations/new.html.erb similarity index 100% rename from app/views/devise/confirmations/new.html.erb rename to app/views/confirmations/new.html.erb diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb deleted file mode 100644 index 0dc6026..0000000 --- a/app/views/devise/registrations/new.html.erb +++ /dev/null @@ -1,54 +0,0 @@ -<div class="row pt-5"> - <div class="col-md-6 offset-md-3 text-box"> - <h2 class="text-center">Sign up</h2> - - <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> - <%= render "devise/shared/error_messages", resource: resource %> - <div class="paragraph"> - <div class="field row", style="margin-top: 10px;"> - <%= f.label :username, class: "col-sm-4 text-sm-end" %><br /> - <%= f.text_field :username, autofocus: true, autocomplete: "username", class:"col-sm-7" %> - </div> - - <div class="field row", style="margin-top: 10px;"> - <%= f.label :firstname, class: "col-sm-4 text-sm-end" %><br /> - <%= f.text_field :firstname, autofocus: true, autocomplete: "firstname", class:"col-sm-7" %> - </div> - - <div class="field row", style="margin-top: 10px;"> - <%= f.label :lastname, class: "col-sm-4 text-sm-end" %><br /> - <%= f.text_field :lastname, autofocus: true, autocomplete: "lastname", class:"col-sm-7" %> - </div> - - <div class="field row", style="margin-top: 10px;"> - <%= f.label :email, class: "col-sm-4 text-sm-end" %><br /> - <%= f.email_field :email, autofocus: true, autocomplete: "email", class:"col-sm-7" %> - </div> - - <div class="field row", style="margin-top: 10px;"> - <%= f.label :password, class: "col-sm-4 text-sm-end" %> - <div class="col-sm-7"> - <div class="row"> - <%= f.password_field :password, autocomplete: "new-password", class:"col" %> - </div> - <% if @minimum_password_length %> - <em>(<%= @minimum_password_length %> characters minimum)</em> - <% end %> - </div> - </div> - - <div class="field row", style="margin-top: 10px;"> - <%= f.label :password_confirmation, class: "col-sm-4 text-sm-end" %><br /> - <%= f.password_field :password_confirmation, autocomplete: "new-password", class:"col-sm-7" %> - </div> - - <div class="actions text-center"> - <%= f.submit "Sign up", class: "btn btn-info text-center margin_top" %> - </div> - </div> - <% end %> - </div> -</div> -<div class="col offset-sm-3"> - <%= link_to "Log In", new_user_session_path, class: "btn btn-info text-center margin_top" %> -</div> diff --git a/app/views/home/home.html.erb b/app/views/home/home.html.erb index e7ed739..0027f7f 100644 --- a/app/views/home/home.html.erb +++ b/app/views/home/home.html.erb @@ -18,7 +18,7 @@ <%= link_to "Logout", home_test_log_out_path, class: 'btn btn-warning btn-md' %> <% else %> <%= link_to "Login", home_test_path, class: 'btn btn-info btn-md' %> - <%= link_to "Sign up", home_test_sign_up_path, class: 'btn btn-info btn-md' %> + <%= link_to "Sign up", sign_up_path, class: 'btn btn-info btn-md' %> <% end %> </body> diff --git a/app/views/devise/mailer/confirmation_instructions.html.erb b/app/views/mailer/confirmation_instructions.html.erb similarity index 100% rename from app/views/devise/mailer/confirmation_instructions.html.erb rename to app/views/mailer/confirmation_instructions.html.erb diff --git a/app/views/devise/mailer/email_changed.html.erb b/app/views/mailer/email_changed.html.erb similarity index 100% rename from app/views/devise/mailer/email_changed.html.erb rename to app/views/mailer/email_changed.html.erb diff --git a/app/views/devise/mailer/password_change.html.erb b/app/views/mailer/password_change.html.erb similarity index 100% rename from app/views/devise/mailer/password_change.html.erb rename to app/views/mailer/password_change.html.erb diff --git a/app/views/devise/mailer/reset_password_instructions.html.erb b/app/views/mailer/reset_password_instructions.html.erb similarity index 100% rename from app/views/devise/mailer/reset_password_instructions.html.erb rename to app/views/mailer/reset_password_instructions.html.erb diff --git a/app/views/devise/mailer/unlock_instructions.html.erb b/app/views/mailer/unlock_instructions.html.erb similarity index 100% rename from app/views/devise/mailer/unlock_instructions.html.erb rename to app/views/mailer/unlock_instructions.html.erb diff --git a/app/views/devise/passwords/edit.html.erb b/app/views/passwords/edit.html.erb similarity index 100% rename from app/views/devise/passwords/edit.html.erb rename to app/views/passwords/edit.html.erb diff --git a/app/views/devise/passwords/new.html.erb b/app/views/passwords/new.html.erb similarity index 100% rename from app/views/devise/passwords/new.html.erb rename to app/views/passwords/new.html.erb diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/registrations/edit.html.erb similarity index 100% rename from app/views/devise/registrations/edit.html.erb rename to app/views/registrations/edit.html.erb diff --git a/app/views/registrations/new.html.erb b/app/views/registrations/new.html.erb new file mode 100644 index 0000000..bdc4140 --- /dev/null +++ b/app/views/registrations/new.html.erb @@ -0,0 +1,47 @@ +<div class="row pt-5"> + <div class="col-md-6 offset-md-3 text-box"> + <h2 class="text-center">Sign up</h2> + + <%= form_for @sign_up_validation, as: :sign_up_validation, url: sign_up_send_path do |f| %> + <div class="paragraph"> + <div class="field row", style="margin-top: 10px;"> + <%= f.label :email, "Email", class: "col-sm-4 text-sm-end" %> + <%= f.email_field :email, class:"col-sm-7", required: true %> + </div> + + <div class="field row", style="margin-top: 10px;"> + <%= f.label :password, "Password", class: "col-sm-4 text-sm-end" %> + <div class="col-sm-7"> + <div class="row"> + <%= f.password_field :password, class:"col", required: true %> + </div> + <% if @minimum_password_length %> + <em>(<%= @minimum_password_length %> characters minimum)</em> + <% end %> + </div> + </div> + + <div class="field row", style="margin-top: 10px;"> + <%= f.label :password_confirmation, "Confirm Password", class: "col-sm-4 text-sm-end" %> + <%= f.password_field :password_confirmation, class:"col-sm-7", required: true %> + </div> + <% if @sign_up_validation.errors.full_messages.any? %> + <% @sign_up_validation.errors.full_messages.each do |error_message| %> + <div class="row field"> + <div class="alert alert-danger error_message offset-sm-4 col-sm-7" role="alert"> + <%= error_message %> + </div> + </div> + <% end %> + <% end %> + + <div class="actions text-center"> + <%= submit_tag "Sign up", class: "btn btn-info text-center margin_top" %> + </div> + </div> + <% end %> + </div> +</div> +<div class="col offset-sm-3"> + <%= link_to "Log In", new_user_path, class: "btn btn-info text-center margin_top" %> +</div> diff --git a/app/views/devise/sessions/new.html.erb b/app/views/sessions/new.html.erb similarity index 100% rename from app/views/devise/sessions/new.html.erb rename to app/views/sessions/new.html.erb diff --git a/app/views/devise/shared/_error_messages.html.erb b/app/views/shared/_error_messages.html.erb similarity index 100% rename from app/views/devise/shared/_error_messages.html.erb rename to app/views/shared/_error_messages.html.erb diff --git a/app/views/devise/shared/_links.html.erb b/app/views/shared/_links.html.erb similarity index 100% rename from app/views/devise/shared/_links.html.erb rename to app/views/shared/_links.html.erb diff --git a/app/views/devise/unlocks/new.html.erb b/app/views/unlocks/new.html.erb similarity index 100% rename from app/views/devise/unlocks/new.html.erb rename to app/views/unlocks/new.html.erb diff --git a/config/application.rb b/config/application.rb index b20ce96..85969a0 100644 --- a/config/application.rb +++ b/config/application.rb @@ -18,5 +18,9 @@ module Calendar # # config.time_zone = "Central Time (US & Canada)" # config.eager_load_paths << Rails.root.join("extras") + + config.action_view.field_error_proc = Proc.new { |html_tag, instance| + html_tag + } end end diff --git a/config/routes.rb b/config/routes.rb index 7f206e4..35ad49d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,9 @@ Rails.application.routes.draw do get 'user', to: 'user#show' get 'users/password/new', to: 'devise/passwords#new', as: 'password_reset' get 'home/test', to: 'home#test' - get 'home/test_sign_up', to: 'home#test_sign_up' get 'home/test_log_out', to: 'home#test_log_out' get 'home/test_auth', to: 'home#test_auth' + get 'sign_up', to: 'registrations#new' + post 'sign_up/send', to: 'registrations#sign_up' + end diff --git a/db/migrate/20220403154425_create_users.rb b/db/migrate/20220403154425_create_users.rb index 504b2a0..e308d34 100644 --- a/db/migrate/20220403154425_create_users.rb +++ b/db/migrate/20220403154425_create_users.rb @@ -1,10 +1,7 @@ class CreateUsers < ActiveRecord::Migration[7.0] def change create_table :users do |t| - t.string :username - t.string :firstname - t.string :lastname - + t.string :auth_id t.timestamps end end diff --git a/db/schema.rb b/db/schema.rb index 5257619..c7b7b53 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,9 +12,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_04_03_154425) do create_table "users", charset: "utf8", force: :cascade do |t| - t.string "username" - t.string "firstname" - t.string "lastname" + t.string "auth_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end diff --git a/test/MessagesTest.rb b/test/MessagesTest.rb new file mode 100644 index 0000000..e95fb80 --- /dev/null +++ b/test/MessagesTest.rb @@ -0,0 +1,5 @@ +class MessagesTest < ActionDispatch::IntegrationTest + test "can' get messages with no auth" do + get "/messages" + assert_response :forbidden + end \ No newline at end of file diff --git a/test/controllers/registrations_controller_test.rb b/test/controllers/registrations_controller_test.rb new file mode 100644 index 0000000..b5b7c37 --- /dev/null +++ b/test/controllers/registrations_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class RegistrationsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end -- GitLab