mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-21 14:19:39 +02:00
Set last_login_at only at login instead of every single action (#1017)
This commit is contained in:
parent
c0e0c2bf62
commit
75ded1c18f
3 changed files with 27 additions and 2 deletions
|
@ -3,13 +3,11 @@ module Authentication
|
||||||
|
|
||||||
included do
|
included do
|
||||||
before_action :authenticate_user!
|
before_action :authenticate_user!
|
||||||
after_action :set_last_login_at, if: -> { Current.user }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class_methods do
|
class_methods do
|
||||||
def skip_authentication(**options)
|
def skip_authentication(**options)
|
||||||
skip_before_action :authenticate_user!, **options
|
skip_before_action :authenticate_user!, **options
|
||||||
skip_after_action :set_last_login_at, **options
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -27,6 +25,7 @@ module Authentication
|
||||||
Current.user = user
|
Current.user = user
|
||||||
reset_session
|
reset_session
|
||||||
session[:user_id] = user.id
|
session[:user_id] = user.id
|
||||||
|
set_last_login_at
|
||||||
end
|
end
|
||||||
|
|
||||||
def logout
|
def logout
|
||||||
|
|
|
@ -24,6 +24,14 @@ class RegistrationsControllerTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "sets last_login_at on successful registration" do
|
||||||
|
post registration_url, params: { user: {
|
||||||
|
email: "john@example.com",
|
||||||
|
password: "password",
|
||||||
|
password_confirmation: "password" } }
|
||||||
|
assert_not_nil User.find_by(email: "john@example.com").last_login_at
|
||||||
|
end
|
||||||
|
|
||||||
test "create when hosted requires an invite code" do
|
test "create when hosted requires an invite code" do
|
||||||
with_env_overrides REQUIRE_INVITE_CODE: "true" do
|
with_env_overrides REQUIRE_INVITE_CODE: "true" do
|
||||||
assert_no_difference "User.count" do
|
assert_no_difference "User.count" do
|
||||||
|
|
18
test/controllers/sessions_controller_test.rb
Normal file
18
test/controllers/sessions_controller_test.rb
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class SessionsControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
setup do
|
||||||
|
@user = users(:family_admin)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "can sign in" do
|
||||||
|
post session_url, params: { email: @user.email, password: "password" }
|
||||||
|
assert_redirected_to root_url
|
||||||
|
end
|
||||||
|
|
||||||
|
test "sets last_login_at on successful login" do
|
||||||
|
assert_changes -> { @user.reload.last_login_at }, from: nil do
|
||||||
|
post session_url, params: { email: @user.email, password: "password" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue