Skip to content

Commit

Permalink
Use CurrentUser::COOKIE instead of just "user_token"
Browse files Browse the repository at this point in the history
... at least where possible. Because, previously, this was the "magic
value" code smell.
  • Loading branch information
pdobb committed Jan 28, 2025
1 parent 7268149 commit 0116dc1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def connect
private

def find_current_user
User.for_token(cookies.signed[:user_token]).take
User.for_token(cookies.signed[CurrentUser::COOKIE]).take
end
end
18 changes: 10 additions & 8 deletions app/models/current_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
# CurrentUser is a Service Object that is responsible for look-up/creation of
# {User} records.
# - Lookup is performed via the "User Token" stored in
# `cookies.signed[:user_token]`.
# `cookies.signed[CurrentUser::COOKIE]`.
# - Creation of a new {User} record generates a "User Token", which is actually
# just the GUID generated by the DB ({User#id}). Which is then "permanently"
# stored into `cookies.signed[:user_token]` for future lookup.
# stored into `cookies.signed[CurrentUser::COOKIE]` for future lookup.
class CurrentUser
COOKIE = :user_token

include CallMethodBehaviors

def initialize(context:)
Expand Down Expand Up @@ -40,7 +42,7 @@ def find
def stored_user_token? = stored_user_token.present?

def stored_user_token
@stored_user_token ||= cookies.signed[:user_token]
@stored_user_token ||= cookies.signed[COOKIE]
end

def create
Expand All @@ -52,7 +54,7 @@ def create
def user_agent = context.user_agent

def store_user_token(value:)
context.store_signed_http_cookie(:user_token, value:)
context.store_signed_http_cookie(COOKIE, value:)
end

# CurrentUser::MigrateToSignedUserTokenCookie is a temporary service object
Expand All @@ -79,17 +81,17 @@ def call

private

def new_signed_user_token? = cookies.signed[:user_token].present?
def new_signed_user_token? = cookies.signed[COOKIE].present?
def old_unsigned_user_token? = old_user_token.present?

def old_user_token = @old_user_token ||= cookies[:user_token]
def old_user_token = @old_user_token ||= cookies[COOKIE]

def delete_old_cookie
cookies.delete(:user_token)
cookies.delete(COOKIE)
end

def store_new_signed_cookie(value:)
context.store_signed_http_cookie(:user_token, value:)
context.store_signed_http_cookie(COOKIE, value:)
end

attr_reader :context
Expand Down

0 comments on commit 0116dc1

Please sign in to comment.