Setup user registration and session management
This commit is contained in:
parent
dde5b3a5d6
commit
5dd9a0c446
|
@ -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
|
|
@ -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
|
|
@ -5,11 +5,32 @@
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||||
<%= csrf_meta_tags %>
|
<%= csrf_meta_tags %>
|
||||||
<%= csp_meta_tag %>
|
<%= csp_meta_tag %>
|
||||||
|
<%= javascript_importmap_tags %>
|
||||||
|
|
||||||
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
|
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<%= yield %>
|
<h1>Summon Player</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li><%= link_to t("home"), root_path %></li>
|
||||||
|
<% if logged_in? %>
|
||||||
|
<li><%= link_to t("profile"), user_path(current_user) %></li>
|
||||||
|
<li><%= link_to t("log_out"), log_out_path, data: { turbo_method: :delete } %></li>
|
||||||
|
<% else %>
|
||||||
|
<li><%= link_to t("log_in"), new_session_path %></li>
|
||||||
|
<li><%= link_to t("register"), new_user_path %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
<main>
|
||||||
|
<% flash.each do |type, msg| %>
|
||||||
|
<div class="flash">
|
||||||
|
<%= "#{type}: #{msg}" %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
<%= yield %>
|
||||||
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -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 %>
|
|
@ -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 %>
|
|
@ -0,0 +1 @@
|
||||||
|
<%= @user.username %>
|
|
@ -1,3 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "active_support/core_ext/integer/time"
|
require "active_support/core_ext/integer/time"
|
||||||
|
|
||||||
Rails.application.configure do
|
Rails.application.configure do
|
||||||
|
@ -60,7 +62,7 @@ Rails.application.configure do
|
||||||
config.assets.quiet = true
|
config.assets.quiet = true
|
||||||
|
|
||||||
# Raises error for missing translations.
|
# 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.
|
# Annotate rendered view with file names.
|
||||||
# config.action_view.annotate_rendered_view_with_filenames = true
|
# config.action_view.annotate_rendered_view_with_filenames = true
|
||||||
|
|
|
@ -53,7 +53,7 @@ Rails.application.configure do
|
||||||
config.active_support.disallowed_deprecation_warnings = []
|
config.active_support.disallowed_deprecation_warnings = []
|
||||||
|
|
||||||
# Raises error for missing translations.
|
# 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.
|
# Annotate rendered view with file names.
|
||||||
# config.action_view.annotate_rendered_view_with_filenames = true
|
# config.action_view.annotate_rendered_view_with_filenames = true
|
||||||
|
|
|
@ -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.
|
|
@ -0,0 +1,3 @@
|
||||||
|
en:
|
||||||
|
users:
|
||||||
|
account_created: Account created!
|
|
@ -0,0 +1,6 @@
|
||||||
|
en:
|
||||||
|
home: Home
|
||||||
|
log_in: Log in
|
||||||
|
log_out: Log out
|
||||||
|
profile: Profile
|
||||||
|
register: Register
|
|
@ -0,0 +1,6 @@
|
||||||
|
en:
|
||||||
|
sessions:
|
||||||
|
new:
|
||||||
|
log_in: Log in
|
||||||
|
destroy:
|
||||||
|
logged_out: You have been logged out
|
|
@ -0,0 +1,5 @@
|
||||||
|
en:
|
||||||
|
users:
|
||||||
|
new:
|
||||||
|
register: Register
|
||||||
|
welcome: Welcome
|
|
@ -1,6 +1,9 @@
|
||||||
Rails.application.routes.draw do
|
# frozen_string_literal: true
|
||||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
|
||||||
|
|
||||||
# Defines the root path route ("/")
|
Rails.application.routes.draw do
|
||||||
# root "articles#index"
|
root "sessions#new"
|
||||||
|
|
||||||
|
resources :users, only: [:new, :create, :show]
|
||||||
|
resources :sessions, only: [:new, :create]
|
||||||
|
delete "log_out", to: "sessions#destroy_session"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
|
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
|
||||||
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
|
driven_by :selenium, using: :headless_firefox, screen_size: [1400, 1400]
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -1,13 +1,11 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
ENV["RAILS_ENV"] ||= "test"
|
ENV["RAILS_ENV"] ||= "test"
|
||||||
require_relative "../config/environment"
|
require_relative "../config/environment"
|
||||||
require "rails/test_help"
|
require "rails/test_help"
|
||||||
|
|
||||||
class ActiveSupport::TestCase
|
class ActiveSupport::TestCase
|
||||||
# Run tests in parallel with specified workers
|
|
||||||
parallelize(workers: :number_of_processors)
|
parallelize(workers: :number_of_processors)
|
||||||
|
|
||||||
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
|
|
||||||
fixtures :all
|
fixtures :all
|
||||||
|
|
||||||
# Add more helper methods to be used by all tests here...
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue