Skip to content

Commit

Permalink
Revert "[Subscriptions] Add/redirect url once token is set in cookie (#…
Browse files Browse the repository at this point in the history
…33437)" (#33528)

This reverts commit 49859cf.
  • Loading branch information
ice9js authored Oct 10, 2023
1 parent 957738c commit 8dbb3e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,20 @@ abstract class Token_Subscription_Service implements Subscription_Service {
* @inheritDoc
*/
public function initialize() {
$this->get_and_set_token_from_request( true );
$this->get_and_set_token_from_request();
}

/**
* Set the token from the Request to the cookie and retrieve the token.
*
* @param bool $redirect_when_cookie_is_set If true, we'll force-reload the page to "hide" the token from the URL.
*
* @return string|null
*/
public function get_and_set_token_from_request( $redirect_when_cookie_is_set = false ) {
public function get_and_set_token_from_request() {
// URL token always has a precedence, so it can overwrite the cookie when new data available.
$token = $this->token_from_request();
if ( $token ) {
$this->set_token_cookie( $token, $redirect_when_cookie_is_set );
} else {
$token = $this->token_from_cookie();

if ( null !== $token ) {
$this->set_token_cookie( $token );
}

return $token;
Expand All @@ -77,7 +74,13 @@ public function visitor_can_view_content( $valid_plan_ids, $access_level ) {
global $current_user;
$old_user = $current_user; // backup the current user so we can set the current user to the token user for paywall purposes

$token = $this->get_and_set_token_from_request( false );
// URL token always has a precedence, so it can overwrite the cookie when new data available.
$token = $this->token_from_request();
if ( $token ) {
$this->set_token_cookie( $token );
} else {
$token = $this->token_from_cookie();
}

$is_valid_token = true;

Expand Down Expand Up @@ -349,22 +352,15 @@ private function token_from_cookie() {
* Store the auth cookie.
*
* @param string $token Auth token.
* @param bool $redirect_when_cookie_is_set If true, we'll force-reload the page to "hide" the token from the URL.
* @return void
*/
private function set_token_cookie( $token, $redirect_when_cookie_is_set = false ) {
private function set_token_cookie( $token ) {
if ( defined( 'TESTING_IN_JETPACK' ) && TESTING_IN_JETPACK ) {
return;
}

if ( ! empty( $token ) && false === headers_sent() ) {
setcookie( self::JWT_AUTH_TOKEN_COOKIE_NAME, $token, 0, '/', COOKIE_DOMAIN, is_ssl(), true ); // httponly -- used by visitor_can_view_content() within the PHP context.
if ( true === $redirect_when_cookie_is_set ) {
$location_without_token = remove_query_arg( 'token' );
if ( wp_safe_redirect( $location_without_token, 302, 'Subscriptions' ) ) {
exit;
}
}
}
}

Expand Down

0 comments on commit 8dbb3e6

Please sign in to comment.