From 88aa54fedd97f6476b2a177822b6ec2b1e1354a4 Mon Sep 17 00:00:00 2001 From: Niklas Trapp Date: Fri, 20 Sep 2024 14:55:30 +0200 Subject: [PATCH] implement basic auth --- app/assets/stylesheets/application.css | 87 ++++++++++++++++--- app/controllers/admin/team_controller.rb | 2 - app/controllers/admin/teams_controller.rb | 2 + app/controllers/admin/user_controller.rb | 2 - app/controllers/admin/users_controller.rb | 6 ++ app/controllers/application_controller.rb | 22 ++++- app/controllers/bike_controller.rb | 2 - app/controllers/bikes_controller.rb | 2 + app/controllers/manage/team_controller.rb | 2 - app/controllers/manage/teams_controller.rb | 2 + app/controllers/pages_controller.rb | 2 + app/controllers/part_controller.rb | 2 - app/controllers/parts_controller.rb | 2 + app/controllers/service_controller.rb | 2 - app/controllers/services_controller.rb | 2 + app/controllers/team_controller.rb | 2 - app/controllers/teams_controller.rb | 2 + app/controllers/user_controller.rb | 2 - app/controllers/user_sessions_controller.rb | 24 +++++ app/controllers/users_controller.rb | 28 ++++++ app/helpers/user_sessions_helper.rb | 2 + app/models/user.rb | 5 +- .../admin/{team => teams}/_form.html.erb | 0 .../admin/{team => teams}/_team.html.erb | 0 app/views/admin/{team => teams}/edit.html.erb | 0 .../admin/{team => teams}/index.html.erb | 0 app/views/admin/{team => teams}/new.html.erb | 0 app/views/admin/{team => teams}/show.html.erb | 0 .../admin/{user => users}/_form.html.erb | 0 .../admin/{user => users}/_user.html.erb | 0 app/views/admin/{user => users}/edit.html.erb | 0 app/views/admin/users/index.html.erb | 22 +++++ app/views/admin/{user => users}/new.html.erb | 0 app/views/admin/{user => users}/show.html.erb | 0 app/views/{bike => bikes}/_bike.html.erb | 0 app/views/{bike => bikes}/_form.html.erb | 0 app/views/{bike => bikes}/edit.html.erb | 0 app/views/{bike => bikes}/index.html.erb | 0 app/views/{bike => bikes}/new.html.erb | 0 app/views/{bike => bikes}/show.html.erb | 0 app/views/layouts/application.html.erb | 18 ++++ .../manage/{team => teams}/_form.html.erb | 0 .../manage/{team => teams}/_team.html.erb | 0 .../manage/{team => teams}/edit.html.erb | 0 .../manage/{team => teams}/index.html.erb | 0 app/views/manage/{team => teams}/new.html.erb | 0 .../manage/{team => teams}/show.html.erb | 0 app/views/pages/index.html.erb | 6 ++ app/views/{part => parts}/_form.html.erb | 0 app/views/{part => parts}/_part.html.erb | 0 app/views/{part => parts}/edit.html.erb | 0 app/views/{part => parts}/index.html.erb | 0 app/views/{part => parts}/new.html.erb | 0 app/views/{part => parts}/show.html.erb | 0 .../{service => services}/_form.html.erb | 0 .../{service => services}/_service.html.erb | 0 app/views/{service => services}/edit.html.erb | 0 .../{service => services}/index.html.erb | 0 app/views/{service => services}/new.html.erb | 0 app/views/{service => services}/show.html.erb | 0 app/views/{team => teams}/_form.html.erb | 0 app/views/{team => teams}/_team.html.erb | 0 app/views/{team => teams}/edit.html.erb | 0 app/views/{team => teams}/index.html.erb | 0 app/views/{team => teams}/new.html.erb | 0 app/views/{team => teams}/show.html.erb | 0 app/views/user/index.html.erb | 16 ---- app/views/user/new.html.erb | 3 - app/views/user/show.html.erb | 10 --- app/views/user_sessions/create.html.erb | 2 + app/views/user_sessions/new.html.erb | 15 ++++ app/views/{user => users}/_form.html.erb | 0 app/views/{user => users}/_user.html.erb | 0 app/views/{user => users}/edit.html.erb | 0 .../{admin/user => users}/index.html.erb | 0 app/views/users/new.html.erb | 33 +++++++ app/views/users/show.html.erb | 11 +++ config/routes.rb | 29 +++++-- db/migrate/20240920065635_create_users.rb | 5 +- ...0920090135_change_team_id_null_in_users.rb | 5 ++ db/schema.rb | 7 +- .../user_sessions_controller_test.rb | 13 +++ 82 files changed, 327 insertions(+), 72 deletions(-) delete mode 100644 app/controllers/admin/team_controller.rb create mode 100644 app/controllers/admin/teams_controller.rb delete mode 100644 app/controllers/admin/user_controller.rb create mode 100644 app/controllers/admin/users_controller.rb delete mode 100644 app/controllers/bike_controller.rb create mode 100644 app/controllers/bikes_controller.rb delete mode 100644 app/controllers/manage/team_controller.rb create mode 100644 app/controllers/manage/teams_controller.rb create mode 100644 app/controllers/pages_controller.rb delete mode 100644 app/controllers/part_controller.rb create mode 100644 app/controllers/parts_controller.rb delete mode 100644 app/controllers/service_controller.rb create mode 100644 app/controllers/services_controller.rb delete mode 100644 app/controllers/team_controller.rb create mode 100644 app/controllers/teams_controller.rb delete mode 100644 app/controllers/user_controller.rb create mode 100644 app/controllers/user_sessions_controller.rb create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/user_sessions_helper.rb rename app/views/admin/{team => teams}/_form.html.erb (100%) rename app/views/admin/{team => teams}/_team.html.erb (100%) rename app/views/admin/{team => teams}/edit.html.erb (100%) rename app/views/admin/{team => teams}/index.html.erb (100%) rename app/views/admin/{team => teams}/new.html.erb (100%) rename app/views/admin/{team => teams}/show.html.erb (100%) rename app/views/admin/{user => users}/_form.html.erb (100%) rename app/views/admin/{user => users}/_user.html.erb (100%) rename app/views/admin/{user => users}/edit.html.erb (100%) create mode 100644 app/views/admin/users/index.html.erb rename app/views/admin/{user => users}/new.html.erb (100%) rename app/views/admin/{user => users}/show.html.erb (100%) rename app/views/{bike => bikes}/_bike.html.erb (100%) rename app/views/{bike => bikes}/_form.html.erb (100%) rename app/views/{bike => bikes}/edit.html.erb (100%) rename app/views/{bike => bikes}/index.html.erb (100%) rename app/views/{bike => bikes}/new.html.erb (100%) rename app/views/{bike => bikes}/show.html.erb (100%) rename app/views/manage/{team => teams}/_form.html.erb (100%) rename app/views/manage/{team => teams}/_team.html.erb (100%) rename app/views/manage/{team => teams}/edit.html.erb (100%) rename app/views/manage/{team => teams}/index.html.erb (100%) rename app/views/manage/{team => teams}/new.html.erb (100%) rename app/views/manage/{team => teams}/show.html.erb (100%) create mode 100644 app/views/pages/index.html.erb rename app/views/{part => parts}/_form.html.erb (100%) rename app/views/{part => parts}/_part.html.erb (100%) rename app/views/{part => parts}/edit.html.erb (100%) rename app/views/{part => parts}/index.html.erb (100%) rename app/views/{part => parts}/new.html.erb (100%) rename app/views/{part => parts}/show.html.erb (100%) rename app/views/{service => services}/_form.html.erb (100%) rename app/views/{service => services}/_service.html.erb (100%) rename app/views/{service => services}/edit.html.erb (100%) rename app/views/{service => services}/index.html.erb (100%) rename app/views/{service => services}/new.html.erb (100%) rename app/views/{service => services}/show.html.erb (100%) rename app/views/{team => teams}/_form.html.erb (100%) rename app/views/{team => teams}/_team.html.erb (100%) rename app/views/{team => teams}/edit.html.erb (100%) rename app/views/{team => teams}/index.html.erb (100%) rename app/views/{team => teams}/new.html.erb (100%) rename app/views/{team => teams}/show.html.erb (100%) delete mode 100644 app/views/user/index.html.erb delete mode 100644 app/views/user/new.html.erb delete mode 100644 app/views/user/show.html.erb create mode 100644 app/views/user_sessions/create.html.erb create mode 100644 app/views/user_sessions/new.html.erb rename app/views/{user => users}/_form.html.erb (100%) rename app/views/{user => users}/_user.html.erb (100%) rename app/views/{user => users}/edit.html.erb (100%) rename app/views/{admin/user => users}/index.html.erb (100%) create mode 100644 app/views/users/new.html.erb create mode 100644 app/views/users/show.html.erb create mode 100644 db/migrate/20240920090135_change_team_id_null_in_users.rb create mode 100644 test/controllers/user_sessions_controller_test.rb diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 288b9ab..f9638c6 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -1,15 +1,78 @@ /* - * This is a manifest file that'll be compiled into application.css, which will include all the files - * listed below. - * - * Any CSS (and SCSS, if configured) file within this directory, lib/assets/stylesheets, or any plugin's - * vendor/assets/stylesheets directory can be referenced here using a relative path. - * - * You're free to add application-wide styles to this file and they'll appear at the bottom of the - * compiled file so the styles you add here take precedence over styles defined in any other CSS - * files in this directory. Styles in this file should be added after the last require_* statement. - * It is generally better to create a new file per style scope. - * - *= require_tree . *= require_self + *= require_tree . */ + +body { + margin: 0; + padding: 0; + height: 100dvh; + font-family: Arial, Helvetica, sans-serif; +} + +#navbar-container { + border-bottom: 1px solid #eee; +} + +#navbar { + display: flex; + justify-content: space-between; + width: 70%; + margin: 0 auto; + padding: 10px; + font-size: 20px; +} + +#navbar-title { + font-weight: 600; +} + +#navbar-items { + display: flex; + gap: 15px; +} + + +/* Login page */ +#login-container { + height: 100dvh; + display: flex; + flex-direction: column; + align-items: center; +} + +#login-inputs { + display: flex; + align-items: center; + flex-direction: column; + gap: 15px; +} + +#login-container .input-div input { + box-sizing: border-box; + width: 100%; + border: 1px solid gray; + padding: 7px 10px; + border-radius: 6px; +} + +#login-button { + margin-top: 10px; + width: 150px; + cursor: pointer; + background-color: #1877f2; + color: white; + font-weight: 600; + font-size: 18px; + padding: 7px 0px; + border: none; + border-radius: 6px; +} + +#login-button:hover { + background-color: #4267b2; +} + +#login-button:active { + background-color: #264177; +} \ No newline at end of file diff --git a/app/controllers/admin/team_controller.rb b/app/controllers/admin/team_controller.rb deleted file mode 100644 index 393d184..0000000 --- a/app/controllers/admin/team_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Admin::TeamController < ApplicationController -end diff --git a/app/controllers/admin/teams_controller.rb b/app/controllers/admin/teams_controller.rb new file mode 100644 index 0000000..b5eed13 --- /dev/null +++ b/app/controllers/admin/teams_controller.rb @@ -0,0 +1,2 @@ +class Admin::TeamsController < ApplicationController +end diff --git a/app/controllers/admin/user_controller.rb b/app/controllers/admin/user_controller.rb deleted file mode 100644 index a238052..0000000 --- a/app/controllers/admin/user_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Admin::UserController < ApplicationController -end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb new file mode 100644 index 0000000..e5150f6 --- /dev/null +++ b/app/controllers/admin/users_controller.rb @@ -0,0 +1,6 @@ +class Admin::UsersController < ApplicationController + + def index + @users = User.all + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0d95db2..08143cf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,24 @@ class ApplicationController < ActionController::Base # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. - allow_browser versions: :modern + # allow_browser versions: :modern + + helper_method :current_user + + def current_user + @current_user ||= session[:user_id] && User.find_by(id: session[:user_id]) + end + + before_action :require_login + + private + + def require_login + unless logged_in? + redirect_to login_path + end + end + + def logged_in? + !!current_user + end end diff --git a/app/controllers/bike_controller.rb b/app/controllers/bike_controller.rb deleted file mode 100644 index fe0f52a..0000000 --- a/app/controllers/bike_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class BikeController < ApplicationController -end diff --git a/app/controllers/bikes_controller.rb b/app/controllers/bikes_controller.rb new file mode 100644 index 0000000..58b2e22 --- /dev/null +++ b/app/controllers/bikes_controller.rb @@ -0,0 +1,2 @@ +class BikesController < ApplicationController +end diff --git a/app/controllers/manage/team_controller.rb b/app/controllers/manage/team_controller.rb deleted file mode 100644 index c4c3e1f..0000000 --- a/app/controllers/manage/team_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Manage::TeamController < ApplicationController -end diff --git a/app/controllers/manage/teams_controller.rb b/app/controllers/manage/teams_controller.rb new file mode 100644 index 0000000..4711013 --- /dev/null +++ b/app/controllers/manage/teams_controller.rb @@ -0,0 +1,2 @@ +class Manage::TeamsController < ApplicationController +end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb new file mode 100644 index 0000000..ce3bf58 --- /dev/null +++ b/app/controllers/pages_controller.rb @@ -0,0 +1,2 @@ +class PagesController < ApplicationController +end diff --git a/app/controllers/part_controller.rb b/app/controllers/part_controller.rb deleted file mode 100644 index 48938e5..0000000 --- a/app/controllers/part_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class PartController < ApplicationController -end diff --git a/app/controllers/parts_controller.rb b/app/controllers/parts_controller.rb new file mode 100644 index 0000000..5d1eb2b --- /dev/null +++ b/app/controllers/parts_controller.rb @@ -0,0 +1,2 @@ +class PartsController < ApplicationController +end diff --git a/app/controllers/service_controller.rb b/app/controllers/service_controller.rb deleted file mode 100644 index 3756bee..0000000 --- a/app/controllers/service_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ServiceController < ApplicationController -end diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb new file mode 100644 index 0000000..2b8a17d --- /dev/null +++ b/app/controllers/services_controller.rb @@ -0,0 +1,2 @@ +class ServicesController < ApplicationController +end diff --git a/app/controllers/team_controller.rb b/app/controllers/team_controller.rb deleted file mode 100644 index 671c085..0000000 --- a/app/controllers/team_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class TeamController < ApplicationController -end diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb new file mode 100644 index 0000000..fa56403 --- /dev/null +++ b/app/controllers/teams_controller.rb @@ -0,0 +1,2 @@ +class TeamsController < ApplicationController +end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb deleted file mode 100644 index 11046fd..0000000 --- a/app/controllers/user_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class UserController < ApplicationController -end diff --git a/app/controllers/user_sessions_controller.rb b/app/controllers/user_sessions_controller.rb new file mode 100644 index 0000000..5edbe18 --- /dev/null +++ b/app/controllers/user_sessions_controller.rb @@ -0,0 +1,24 @@ +class UserSessionsController < ApplicationController + # Login is not required for these actions + skip_before_action :require_login, only: [:new, :create] + def new + @user = User.new + end + + def create + @user = User.find_by(email: params[:user][:email]) + + if @user && @user.authenticate(params[:user][:password]) + session[:user_id] = @user.id + redirect_to root_path + else + flash[:alert] = "Login failed" + redirect_to new_user_session_path + end + end + + def destroy + session[:user_id] = nil + redirect_to login_path + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..8dac4c5 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,28 @@ +class UsersController < ApplicationController + skip_before_action :require_login, only: [:new, :create] + def show + @user = current_user + end + + def new + @user = User.new + end + + def create + @user = User.new(user_params) + + if @user.save + flash[:notice] = "User created successfully" + redirect_to users_path + else + flash[:alert] = "User not created" + render :new, status: :unprocessable_entity + end + end + + private + + def user_params + params.require(:user).permit(:email, :name, :password, :password_confirmation) + end +end diff --git a/app/helpers/user_sessions_helper.rb b/app/helpers/user_sessions_helper.rb new file mode 100644 index 0000000..2018402 --- /dev/null +++ b/app/helpers/user_sessions_helper.rb @@ -0,0 +1,2 @@ +module UserSessionsHelper +end diff --git a/app/models/user.rb b/app/models/user.rb index 9d3f9f2..67da31c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,6 @@ class User < ApplicationRecord - belongs_to :team + has_secure_password + validates :email, presence: true, uniqueness: true + validates :name, presence: true, uniqueness: true + belongs_to :team, optional: true end diff --git a/app/views/admin/team/_form.html.erb b/app/views/admin/teams/_form.html.erb similarity index 100% rename from app/views/admin/team/_form.html.erb rename to app/views/admin/teams/_form.html.erb diff --git a/app/views/admin/team/_team.html.erb b/app/views/admin/teams/_team.html.erb similarity index 100% rename from app/views/admin/team/_team.html.erb rename to app/views/admin/teams/_team.html.erb diff --git a/app/views/admin/team/edit.html.erb b/app/views/admin/teams/edit.html.erb similarity index 100% rename from app/views/admin/team/edit.html.erb rename to app/views/admin/teams/edit.html.erb diff --git a/app/views/admin/team/index.html.erb b/app/views/admin/teams/index.html.erb similarity index 100% rename from app/views/admin/team/index.html.erb rename to app/views/admin/teams/index.html.erb diff --git a/app/views/admin/team/new.html.erb b/app/views/admin/teams/new.html.erb similarity index 100% rename from app/views/admin/team/new.html.erb rename to app/views/admin/teams/new.html.erb diff --git a/app/views/admin/team/show.html.erb b/app/views/admin/teams/show.html.erb similarity index 100% rename from app/views/admin/team/show.html.erb rename to app/views/admin/teams/show.html.erb diff --git a/app/views/admin/user/_form.html.erb b/app/views/admin/users/_form.html.erb similarity index 100% rename from app/views/admin/user/_form.html.erb rename to app/views/admin/users/_form.html.erb diff --git a/app/views/admin/user/_user.html.erb b/app/views/admin/users/_user.html.erb similarity index 100% rename from app/views/admin/user/_user.html.erb rename to app/views/admin/users/_user.html.erb diff --git a/app/views/admin/user/edit.html.erb b/app/views/admin/users/edit.html.erb similarity index 100% rename from app/views/admin/user/edit.html.erb rename to app/views/admin/users/edit.html.erb diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb new file mode 100644 index 0000000..b382efe --- /dev/null +++ b/app/views/admin/users/index.html.erb @@ -0,0 +1,22 @@ +

All Users

+ +<%= link_to 'New user', new_user_path %> + + + + + + + + + + + <% @users.each do |user| %> + + + + + + <% end %> + +
idemailname
<%= user.id %><%= user.email %><%= user.name %>
\ No newline at end of file diff --git a/app/views/admin/user/new.html.erb b/app/views/admin/users/new.html.erb similarity index 100% rename from app/views/admin/user/new.html.erb rename to app/views/admin/users/new.html.erb diff --git a/app/views/admin/user/show.html.erb b/app/views/admin/users/show.html.erb similarity index 100% rename from app/views/admin/user/show.html.erb rename to app/views/admin/users/show.html.erb diff --git a/app/views/bike/_bike.html.erb b/app/views/bikes/_bike.html.erb similarity index 100% rename from app/views/bike/_bike.html.erb rename to app/views/bikes/_bike.html.erb diff --git a/app/views/bike/_form.html.erb b/app/views/bikes/_form.html.erb similarity index 100% rename from app/views/bike/_form.html.erb rename to app/views/bikes/_form.html.erb diff --git a/app/views/bike/edit.html.erb b/app/views/bikes/edit.html.erb similarity index 100% rename from app/views/bike/edit.html.erb rename to app/views/bikes/edit.html.erb diff --git a/app/views/bike/index.html.erb b/app/views/bikes/index.html.erb similarity index 100% rename from app/views/bike/index.html.erb rename to app/views/bikes/index.html.erb diff --git a/app/views/bike/new.html.erb b/app/views/bikes/new.html.erb similarity index 100% rename from app/views/bike/new.html.erb rename to app/views/bikes/new.html.erb diff --git a/app/views/bike/show.html.erb b/app/views/bikes/show.html.erb similarity index 100% rename from app/views/bike/show.html.erb rename to app/views/bikes/show.html.erb diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 6729963..bdd2933 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -18,6 +18,24 @@ + <% flash.each do |type, msg| %> +
+ <%= msg %> +
+ <% end %> + + <% if current_user %> + + <% end %> + <%= yield %> diff --git a/app/views/manage/team/_form.html.erb b/app/views/manage/teams/_form.html.erb similarity index 100% rename from app/views/manage/team/_form.html.erb rename to app/views/manage/teams/_form.html.erb diff --git a/app/views/manage/team/_team.html.erb b/app/views/manage/teams/_team.html.erb similarity index 100% rename from app/views/manage/team/_team.html.erb rename to app/views/manage/teams/_team.html.erb diff --git a/app/views/manage/team/edit.html.erb b/app/views/manage/teams/edit.html.erb similarity index 100% rename from app/views/manage/team/edit.html.erb rename to app/views/manage/teams/edit.html.erb diff --git a/app/views/manage/team/index.html.erb b/app/views/manage/teams/index.html.erb similarity index 100% rename from app/views/manage/team/index.html.erb rename to app/views/manage/teams/index.html.erb diff --git a/app/views/manage/team/new.html.erb b/app/views/manage/teams/new.html.erb similarity index 100% rename from app/views/manage/team/new.html.erb rename to app/views/manage/teams/new.html.erb diff --git a/app/views/manage/team/show.html.erb b/app/views/manage/teams/show.html.erb similarity index 100% rename from app/views/manage/team/show.html.erb rename to app/views/manage/teams/show.html.erb diff --git a/app/views/pages/index.html.erb b/app/views/pages/index.html.erb new file mode 100644 index 0000000..94e635b --- /dev/null +++ b/app/views/pages/index.html.erb @@ -0,0 +1,6 @@ +<% if current_user %> +

Welcome, <%= current_user.name %>

+<% else %> +

This is the index page

+ <%= link_to 'Login', new_user_session_path %>
+<% end %> \ No newline at end of file diff --git a/app/views/part/_form.html.erb b/app/views/parts/_form.html.erb similarity index 100% rename from app/views/part/_form.html.erb rename to app/views/parts/_form.html.erb diff --git a/app/views/part/_part.html.erb b/app/views/parts/_part.html.erb similarity index 100% rename from app/views/part/_part.html.erb rename to app/views/parts/_part.html.erb diff --git a/app/views/part/edit.html.erb b/app/views/parts/edit.html.erb similarity index 100% rename from app/views/part/edit.html.erb rename to app/views/parts/edit.html.erb diff --git a/app/views/part/index.html.erb b/app/views/parts/index.html.erb similarity index 100% rename from app/views/part/index.html.erb rename to app/views/parts/index.html.erb diff --git a/app/views/part/new.html.erb b/app/views/parts/new.html.erb similarity index 100% rename from app/views/part/new.html.erb rename to app/views/parts/new.html.erb diff --git a/app/views/part/show.html.erb b/app/views/parts/show.html.erb similarity index 100% rename from app/views/part/show.html.erb rename to app/views/parts/show.html.erb diff --git a/app/views/service/_form.html.erb b/app/views/services/_form.html.erb similarity index 100% rename from app/views/service/_form.html.erb rename to app/views/services/_form.html.erb diff --git a/app/views/service/_service.html.erb b/app/views/services/_service.html.erb similarity index 100% rename from app/views/service/_service.html.erb rename to app/views/services/_service.html.erb diff --git a/app/views/service/edit.html.erb b/app/views/services/edit.html.erb similarity index 100% rename from app/views/service/edit.html.erb rename to app/views/services/edit.html.erb diff --git a/app/views/service/index.html.erb b/app/views/services/index.html.erb similarity index 100% rename from app/views/service/index.html.erb rename to app/views/services/index.html.erb diff --git a/app/views/service/new.html.erb b/app/views/services/new.html.erb similarity index 100% rename from app/views/service/new.html.erb rename to app/views/services/new.html.erb diff --git a/app/views/service/show.html.erb b/app/views/services/show.html.erb similarity index 100% rename from app/views/service/show.html.erb rename to app/views/services/show.html.erb diff --git a/app/views/team/_form.html.erb b/app/views/teams/_form.html.erb similarity index 100% rename from app/views/team/_form.html.erb rename to app/views/teams/_form.html.erb diff --git a/app/views/team/_team.html.erb b/app/views/teams/_team.html.erb similarity index 100% rename from app/views/team/_team.html.erb rename to app/views/teams/_team.html.erb diff --git a/app/views/team/edit.html.erb b/app/views/teams/edit.html.erb similarity index 100% rename from app/views/team/edit.html.erb rename to app/views/teams/edit.html.erb diff --git a/app/views/team/index.html.erb b/app/views/teams/index.html.erb similarity index 100% rename from app/views/team/index.html.erb rename to app/views/teams/index.html.erb diff --git a/app/views/team/new.html.erb b/app/views/teams/new.html.erb similarity index 100% rename from app/views/team/new.html.erb rename to app/views/teams/new.html.erb diff --git a/app/views/team/show.html.erb b/app/views/teams/show.html.erb similarity index 100% rename from app/views/team/show.html.erb rename to app/views/teams/show.html.erb diff --git a/app/views/user/index.html.erb b/app/views/user/index.html.erb deleted file mode 100644 index f5ac45c..0000000 --- a/app/views/user/index.html.erb +++ /dev/null @@ -1,16 +0,0 @@ -

<%= notice %>

- -<% content_for :title, "Users" %> - -

Users

- -
- <% @users.each do |user| %> - <%= render user %> -

- <%= link_to "Show this user", user %> -

- <% end %> -
- -<%= link_to "New user", new_user_path %> diff --git a/app/views/user/new.html.erb b/app/views/user/new.html.erb deleted file mode 100644 index 6b377b6..0000000 --- a/app/views/user/new.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -<% content_for :title, "New user" %> - -

New user

diff --git a/app/views/user/show.html.erb b/app/views/user/show.html.erb deleted file mode 100644 index 673fae2..0000000 --- a/app/views/user/show.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

<%= notice %>

- -<%= render @user %> - -
- <%= link_to "Edit this user", edit_user_path(@user) %> | - <%= link_to "Back to users", users_path %> - - <%= button_to "Destroy this user", @user, method: :delete %> -
diff --git a/app/views/user_sessions/create.html.erb b/app/views/user_sessions/create.html.erb new file mode 100644 index 0000000..b25f738 --- /dev/null +++ b/app/views/user_sessions/create.html.erb @@ -0,0 +1,2 @@ +

UserSessions#create

+

Find me in app/views/user_sessions/create.html.erb

diff --git a/app/views/user_sessions/new.html.erb b/app/views/user_sessions/new.html.erb new file mode 100644 index 0000000..da037cc --- /dev/null +++ b/app/views/user_sessions/new.html.erb @@ -0,0 +1,15 @@ +
+

Motorify

+
Login
+<%= form_with model: @user, id:'login-inputs', url: user_sessions_path do |f| %> +
+ <%= f.label :email %>
+ <%= f.text_field :email %> +
+
+ <%= f.label :password %>
+ <%= f.password_field :password %> +
+ <%= f.submit 'Login', id:'login-button' %> +<% end %> +
\ No newline at end of file diff --git a/app/views/user/_form.html.erb b/app/views/users/_form.html.erb similarity index 100% rename from app/views/user/_form.html.erb rename to app/views/users/_form.html.erb diff --git a/app/views/user/_user.html.erb b/app/views/users/_user.html.erb similarity index 100% rename from app/views/user/_user.html.erb rename to app/views/users/_user.html.erb diff --git a/app/views/user/edit.html.erb b/app/views/users/edit.html.erb similarity index 100% rename from app/views/user/edit.html.erb rename to app/views/users/edit.html.erb diff --git a/app/views/admin/user/index.html.erb b/app/views/users/index.html.erb similarity index 100% rename from app/views/admin/user/index.html.erb rename to app/views/users/index.html.erb diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 0000000..946ab93 --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1,33 @@ +

Sign up

+ +<%= form_with model: @user do |f| %> + <% if @user.errors.any? %> +
+ +
+ <% end %> +
+ <%= f.label :email %>
+ <%= f.text_field :email %> +
+
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :password %>
+ <%= f.password_field :password %> +
+
+ <%= f.label :password_confirmation %>
+ <%= f.password_field :password_confirmation %> +
+

+ <%= f.submit %> +

+<% end %> + \ No newline at end of file diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000..a81348c --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,11 @@ +

<%= notice %>

+ +<%= @user.name %> +<%= @user.email %> +<%= @user.team_id %> + +
+ <%= link_to "Change password", edit_user_path(@user) %> + <%= link_to "Edit", edit_user_path(@user) %> + <%= link_to "Back", root_path %> +
diff --git a/config/routes.rb b/config/routes.rb index 9544783..2e7c865 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + get "user_sessions/new" + get "user_sessions/create" # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. @@ -12,23 +14,34 @@ # Defines the root path route ("/") # root "posts#index" - resources :team, only: [:show] + root 'pages#index' - resources :user, only: [:show, :new, :create, :edit, :update, :destroy] + resources :teams, only: [:show] - resources :bike + resources :users, only: [:show, :new, :create, :update, :edit] - resources :part + resources :bikes - resources :service + resources :parts + + resources :services + + resources :user_sessions, only: [:new, :create, :destroy] namespace :manage do - resources :team + resources :teams end namespace :admin do - resources :team - resources :user + resources :teams + resources :users end + # Bind better urls + get '/login', to: 'user_sessions#new' + get '/register', to: 'users#new' + get '/logout', to: 'user_sessions#destroy' + + get '/profile', to: 'users#show' + end diff --git a/db/migrate/20240920065635_create_users.rb b/db/migrate/20240920065635_create_users.rb index 08954f8..f9664b4 100644 --- a/db/migrate/20240920065635_create_users.rb +++ b/db/migrate/20240920065635_create_users.rb @@ -3,9 +3,10 @@ def change create_table :users do |t| t.string :email t.string :name - t.string :password + t.string :password_digest + t.string :password_confirmation t.string :role - t.references :team, null: false, foreign_key: true + t.references :team, null: true, foreign_key: true t.timestamps end diff --git a/db/migrate/20240920090135_change_team_id_null_in_users.rb b/db/migrate/20240920090135_change_team_id_null_in_users.rb new file mode 100644 index 0000000..06e64b5 --- /dev/null +++ b/db/migrate/20240920090135_change_team_id_null_in_users.rb @@ -0,0 +1,5 @@ +class ChangeTeamIdNullInUsers < ActiveRecord::Migration[6.0] + def change + change_column_null :users, :team_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 0ee3eda..7fb33c0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_09_20_071102) do +ActiveRecord::Schema[7.2].define(version: 2024_09_20_090135) do create_table "bikes", force: :cascade do |t| t.string "manufacturer" t.string "model" @@ -63,9 +63,10 @@ create_table "users", force: :cascade do |t| t.string "email" t.string "name" - t.string "password" + t.string "password_digest" + t.string "password_confirmation" t.string "role" - t.integer "team_id", null: false + t.integer "team_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["team_id"], name: "index_users_on_team_id" diff --git a/test/controllers/user_sessions_controller_test.rb b/test/controllers/user_sessions_controller_test.rb new file mode 100644 index 0000000..f413758 --- /dev/null +++ b/test/controllers/user_sessions_controller_test.rb @@ -0,0 +1,13 @@ +require "test_helper" + +class UserSessionsControllerTest < ActionDispatch::IntegrationTest + test "should get new" do + get user_sessions_new_url + assert_response :success + end + + test "should get create" do + get user_sessions_create_url + assert_response :success + end +end