0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/lib/current_user.rb
Régis Hanol fba56920b5
FEATURE: Prompt anonymous users to sign up after engagement clicks (#40256)
Previously, anonymous users clicking event RSVP buttons hit a dead-end
with no signup prompt, and other engagement actions (Like, React, Vote)
opened the login modal but lost the click on the way to authentication.

This change captures the intent in a short-lived signed cookie via `POST
/anonymous-action` and replays it through each action's existing service
when `CurrentUser#log_on_user` completes, so anonymous engagement
converts to signups with the original action carried through.

Ref - t/184288

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 09:30:38 +02:00

60 lines
1.4 KiB
Ruby
Vendored

# frozen_string_literal: true
module CurrentUser
def self.has_auth_cookie?(env)
Discourse.current_user_provider.new(env).has_auth_cookie?
end
def self.lookup_from_env(env)
Discourse.current_user_provider.new(env).current_user
end
# can be used to pretend current user does no exist, for CSRF attacks
def clear_current_user
@current_user_provider = Discourse.current_user_provider.new({})
end
def log_on_user(user, opts = {}, replay_anonymous_action: false)
current_user_provider.log_on_user(user, session, cookies, opts)
user.logged_in
AnonymousAction.consume(user, cookies) if replay_anonymous_action
end
def log_off_user(push_subscription: nil)
current_user_provider.log_off_user(session, cookies, push_subscription:)
end
def start_impersonating_user(user)
current_user_provider.start_impersonating_user(user)
end
def stop_impersonating_user
current_user_provider.stop_impersonating_user
end
def is_api?
current_user_provider.is_api?
end
def is_user_api?
current_user_provider.is_user_api?
end
def current_user
current_user_provider.current_user
end
def impersonation_acting_user
current_user_provider.impersonation_acting_user
end
def refresh_session(user)
current_user_provider.refresh_session(user, session, cookies)
end
private
def current_user_provider
@current_user_provider ||= Discourse.current_user_provider.new(request.env)
end
end