Compare commits

...

4 Commits

Author SHA1 Message Date
0919a26686 Changed authorize to authorize_user!
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
2024-08-25 21:26:35 +02:00
abe9267893 Modified authentication with user_signed_in? and current_user
Some checks are pending
CI / scan_ruby (push) Waiting to run
CI / scan_js (push) Waiting to run
CI / lint (push) Waiting to run
CI / test (push) Waiting to run
2024-08-25 01:20:55 +02:00
0b4825dcbb Updated test 2024-08-25 00:57:05 +02:00
bc05f9c984 fixed tests 2024-08-25 00:46:43 +02:00
11 changed files with 49 additions and 40 deletions

View File

@@ -1,12 +1,26 @@
class ApplicationController < ActionController::Base
before_action :set_current_request_details
before_action :authenticate
before_action :authenticate_user!
private
def authenticate
if session_record = Session.find_by_id(cookies.signed[:session_token])
Current.session = session_record
else
def current_user
Current.user || authenticate_user_from_session
end
helper_method :current_user
def authenticate_user_from_session
session_record = Session.find_by_id(cookies.signed[:session_token])
Current.session = session_record
Current.user
end
def user_signed_in?
current_user.present?
end
helper_method :user_signed_in?
def authenticate_user!
unless user_signed_in?
redirect_to sign_in_path
end
end

View File

@@ -1,4 +1,5 @@
class HomeController < ApplicationController
skip_before_action :authenticate_user!
def index
end
end

View File

@@ -1,6 +1,5 @@
class Identity::EmailVerificationsController < ApplicationController
skip_before_action :authenticate, only: :show
skip_before_action :authenticate_user!, only: :show
before_action :set_user, only: :show
def show

View File

@@ -1,6 +1,5 @@
class Identity::PasswordResetsController < ApplicationController
skip_before_action :authenticate
skip_before_action :authenticate_user!
before_action :set_user, only: %i[ edit update ]
def new

View File

@@ -1,5 +1,5 @@
class RegistrationsController < ApplicationController
skip_before_action :authenticate
skip_before_action :authenticate_user!
def new
@user = User.new

View File

@@ -1,6 +1,5 @@
class SessionsController < ApplicationController
skip_before_action :authenticate, only: %i[ new create ]
skip_before_action :authenticate_user!, only: %i[ new create ]
before_action :set_session, only: :destroy
def index

View File

@@ -1,6 +1,6 @@
class VehiclesController < ApplicationController
before_action :set_vehicle, only: %i[ show edit update destroy ]
skip_before_action :authenticate
skip_before_action :authenticate_user!
# has_many_attached :images

View File

@@ -1,23 +1,19 @@
<p style="color: green"><%= notice %></p>
<p>Signed as <%= Current.user.email %></p>
<h2>Login and verification</h2>
<div>
<%= link_to "Change password", edit_password_path %>
</div>
<div>
<%= link_to "Change email address", edit_identity_email_path %>
</div>
<h2>Access history</h2>
<div>
<%= link_to "Devices & Sessions", sessions_path %>
</div>
<br>
<%= button_to "Log out", Current.session, method: :delete %>
<% if user_signed_in? %>
<p>Signed as <%= current_user.email %></p>
<h2>Login and verification</h2>
<div>
<%= link_to "Change password", edit_password_path %>
</div>
<div>
<%= link_to "Change email address", edit_identity_email_path %>
</div>
<h2>Access history</h2>
<div>
<%= link_to "Devices & Sessions", sessions_path %>
</div>
<br>
<%= button_to "Log out", Current.session, method: :delete %>
<% else %>
<%= link_to "Log in", sign_in_path %>
<% end %>

View File

@@ -31,7 +31,8 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest
assert_equal "That email or password is incorrect", flash[:alert]
get root_url
assert_redirected_to sign_in_url
# assert_redirected_to sign_in_url
assert_response :success
end
test "should sign out" do

View File

@@ -17,7 +17,7 @@ class VehiclesControllerTest < ActionDispatch::IntegrationTest
test "should create vehicle" do
assert_difference("Vehicle.count") do
post vehicles_url, params: { vehicle: { car_brand: @vehicle.car_brand, fuel_type: @vehicle.fuel_type, license_plate: @vehicle.license_plate, model: @vehicle.model, model_year: @vehicle.model_year, type: @vehicle.type } }
post vehicles_url, params: { vehicle: { car_brand: @vehicle.car_brand, fuel_type: @vehicle.fuel_type, license_plate: @vehicle.license_plate, model: @vehicle.model, model_year: @vehicle.model_year, vehicle_type: @vehicle.vehicle_type } }
end
assert_redirected_to vehicle_url(Vehicle.last)
@@ -34,7 +34,7 @@ class VehiclesControllerTest < ActionDispatch::IntegrationTest
end
test "should update vehicle" do
patch vehicle_url(@vehicle), params: { vehicle: { car_brand: @vehicle.car_brand, fuel_type: @vehicle.fuel_type, license_plate: @vehicle.license_plate, model: @vehicle.model, model_year: @vehicle.model_year, type: @vehicle.type } }
patch vehicle_url(@vehicle), params: { vehicle: { car_brand: @vehicle.car_brand, fuel_type: @vehicle.fuel_type, license_plate: @vehicle.license_plate, model: @vehicle.model, model_year: @vehicle.model_year, vehicle_type: @vehicle.vehicle_type } }
assert_redirected_to vehicle_url(@vehicle)
end

View File

@@ -2,7 +2,7 @@
one:
license_plate: MyString
type:
vehicle_type:
model_year: 2024-08-20 18:58:36
car_brand: MyString
model: MyString
@@ -10,7 +10,7 @@ one:
two:
license_plate: MyString
type:
vehicle_type:
model_year: 2024-08-20 18:58:36
car_brand: MyString
model: MyString