From 5dd9a0c4469896cde5b42a36353dbf8276e45004 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Tue, 15 Aug 2023 14:50:39 +0100 Subject: [PATCH] Setup user registration and session management --- app/controllers/sessions_controller.rb | 21 ++++++++++++ app/controllers/users_controller.rb | 35 ++++++++++++++++++++ app/views/layouts/application.html.erb | 23 ++++++++++++- app/views/sessions/new.html.erb | 11 ++++++ app/views/users/new.html.erb | 22 ++++++++++++ app/views/users/show.html.erb | 1 + config/environments/development.rb | 4 ++- config/environments/test.rb | 2 +- config/locales/controllers/sessions/en.yml | 6 ++++ config/locales/controllers/users/en.yml | 3 ++ config/locales/views/layouts/application.yml | 6 ++++ config/locales/views/sessions/en.yml | 6 ++++ config/locales/views/users/en.yml | 5 +++ config/routes.rb | 11 +++--- test/application_system_test_case.rb | 4 ++- test/system/new_session_test.rb | 19 +++++++++++ test/system/register_users_test.rb | 15 +++++++++ test/test_helper.rb | 6 ++-- 18 files changed, 188 insertions(+), 12 deletions(-) create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/controllers/users_controller.rb create mode 100644 app/views/sessions/new.html.erb create mode 100644 app/views/users/new.html.erb create mode 100644 app/views/users/show.html.erb create mode 100644 config/locales/controllers/sessions/en.yml create mode 100644 config/locales/controllers/users/en.yml create mode 100644 config/locales/views/layouts/application.yml create mode 100644 config/locales/views/sessions/en.yml create mode 100644 config/locales/views/users/en.yml create mode 100644 test/system/new_session_test.rb create mode 100644 test/system/register_users_test.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..0f400e3 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class SessionsController < ApplicationController + def new; end + + def create + @user = User.find_by(username: params[:username]) + + if @user.present? && @user.authenticate(params[:password]) + session[:user_id] = @user.id + redirect_to @user, notice: t(".logged_in") + else + redirect_to root_path, notice: t(".login_fail") + end + end + + def destroy_session + reset_session + redirect_to root_path, notice: t(".logged_out") + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..7282f11 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +class UsersController < ApplicationController + def new + @user = User.new + end + + def create + @user = User.new(user_params) + if @user.save + # TODO: Add email confirmation workflow + session[:user_id] = @user.id + redirect_to root_path, notice: t(".account_created") + else + render :new + end + end + + def show + @user = User.find(params[:id]) + end + + private + + def user_params + params.require(:user).permit( + :username, + :email, + :password, + :password_confirmation, + :first_name, + :last_names, + ) + end +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4eaf80c..8bb64ea 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,11 +5,32 @@ <%= csrf_meta_tags %> <%= csp_meta_tag %> + <%= javascript_importmap_tags %> <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> - <%= yield %> +

Summon Player

+ +
+ <% flash.each do |type, msg| %> +
+ <%= "#{type}: #{msg}" %> +
+ <% end %> + <%= yield %> +
diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb new file mode 100644 index 0000000..16776a5 --- /dev/null +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,11 @@ +<%= t ".log_in" %> + +<%= form_with url: sessions_path, method: :post do |f| %> + <%= f.label :username %> + <%= f.text_field :username %> + + <%= f.label :password %> + <%= f.password_field :password %> + + <%= f.submit t(".log_in") %> +<% end %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 0000000..282ab0b --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1,22 @@ + +<%= form_with model: @user do |f| %> + <%= f.label :username %> + <%= f.text_field :username %> + + <%= f.label :email %> + <%= f.text_field :email %> + + <%= f.label :first_name %> + <%= f.text_field :first_name %> + + <%= f.label :last_names %> + <%= f.text_field :last_names %> + + <%= f.label :password %> + <%= f.password_field :password %> + + <%= f.label :password_confirmation %> + <%= f.password_field :password_confirmation %> + + <%= f.submit t(".register") %> +<% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000..2853777 --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1 @@ +<%= @user.username %> diff --git a/config/environments/development.rb b/config/environments/development.rb index 8500f45..685ef2b 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "active_support/core_ext/integer/time" Rails.application.configure do @@ -60,7 +62,7 @@ Rails.application.configure do config.assets.quiet = true # Raises error for missing translations. - # config.i18n.raise_on_missing_translations = true + config.i18n.raise_on_missing_translations = true # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true diff --git a/config/environments/test.rb b/config/environments/test.rb index 6ea4d1e..52f35f9 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -53,7 +53,7 @@ Rails.application.configure do config.active_support.disallowed_deprecation_warnings = [] # Raises error for missing translations. - # config.i18n.raise_on_missing_translations = true + config.i18n.raise_on_missing_translations = true # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true diff --git a/config/locales/controllers/sessions/en.yml b/config/locales/controllers/sessions/en.yml new file mode 100644 index 0000000..49bcdbf --- /dev/null +++ b/config/locales/controllers/sessions/en.yml @@ -0,0 +1,6 @@ +en: + sessions: + logged_in: You’ve been logged in. + login_fail: Sorry, we couldn’t log you in. + destroy: + logged_out: You have been logged out. diff --git a/config/locales/controllers/users/en.yml b/config/locales/controllers/users/en.yml new file mode 100644 index 0000000..413e9f8 --- /dev/null +++ b/config/locales/controllers/users/en.yml @@ -0,0 +1,3 @@ +en: + users: + account_created: Account created! diff --git a/config/locales/views/layouts/application.yml b/config/locales/views/layouts/application.yml new file mode 100644 index 0000000..3b7195a --- /dev/null +++ b/config/locales/views/layouts/application.yml @@ -0,0 +1,6 @@ +en: + home: Home + log_in: Log in + log_out: Log out + profile: Profile + register: Register diff --git a/config/locales/views/sessions/en.yml b/config/locales/views/sessions/en.yml new file mode 100644 index 0000000..c64372b --- /dev/null +++ b/config/locales/views/sessions/en.yml @@ -0,0 +1,6 @@ +en: + sessions: + new: + log_in: Log in + destroy: + logged_out: You have been logged out diff --git a/config/locales/views/users/en.yml b/config/locales/views/users/en.yml new file mode 100644 index 0000000..d22d34a --- /dev/null +++ b/config/locales/views/users/en.yml @@ -0,0 +1,5 @@ +en: + users: + new: + register: Register + welcome: Welcome diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..be54046 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,9 @@ -Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html +# frozen_string_literal: true - # Defines the root path route ("/") - # root "articles#index" +Rails.application.routes.draw do + root "sessions#new" + + resources :users, only: [:new, :create, :show] + resources :sessions, only: [:new, :create] + delete "log_out", to: "sessions#destroy_session" end diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index d19212a..3f27814 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require "test_helper" class ApplicationSystemTestCase < ActionDispatch::SystemTestCase - driven_by :selenium, using: :chrome, screen_size: [1400, 1400] + driven_by :selenium, using: :headless_firefox, screen_size: [1400, 1400] end diff --git a/test/system/new_session_test.rb b/test/system/new_session_test.rb new file mode 100644 index 0000000..a46bdea --- /dev/null +++ b/test/system/new_session_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "application_system_test_case" + +class SessionsTest < ApplicationSystemTestCase + setup do + @user = users(:user) + end + + test "can log in and log out existing user" do + visit new_session_path + fill_in "username", with: @user.username + fill_in "password", with: "tolkien-abercrombie-hobb-barker" + click_button I18n.t("log_in") + assert_text I18n.t("sessions.logged_in") + click_link I18n.t("log_out") + assert_text I18n.t("sessions.logged_out") + end +end diff --git a/test/system/register_users_test.rb b/test/system/register_users_test.rb new file mode 100644 index 0000000..e83755a --- /dev/null +++ b/test/system/register_users_test.rb @@ -0,0 +1,15 @@ +require "application_system_test_case" + +class RegisterUsersTest < ApplicationSystemTestCase + test "can register a new user" do + visit new_user_path + fill_in "user_username", with: "legolas" + fill_in "user_email", with: "legolas@example.com" + fill_in "user_first_name", with: "Legolas" + fill_in "user_last_names", with: "Greenleaf" + fill_in "user_password", with: "mirkwood-mordor-shire" + fill_in "user_password_confirmation", with: "mirkwood-mordor-shire" + click_button I18n.t("users.new.register") + assert_text I18n.t("users.account_created") + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index d713e37..38efdc9 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,13 +1,11 @@ +# frozen_string_literal: true + ENV["RAILS_ENV"] ||= "test" require_relative "../config/environment" require "rails/test_help" class ActiveSupport::TestCase - # Run tests in parallel with specified workers parallelize(workers: :number_of_processors) - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all - - # Add more helper methods to be used by all tests here... end