diff --git a/src/Actions/Authentication.php b/src/Actions/Authentication.php index f6fb41c94..54100c299 100644 --- a/src/Actions/Authentication.php +++ b/src/Actions/Authentication.php @@ -59,7 +59,10 @@ public function createAccountConnection(WP_User $wpUser, string $connection): vo $found = false; wp_cache_get($cacheKey, '', false, $found); - if (! $found && false === get_transient($cacheKey)) { + if (! $found || false === get_transient($cacheKey)) { + set_transient($cacheKey, $wpUser->ID, 120); + wp_cache_set($cacheKey, $wpUser->ID, '', 120); + $database = $this->getPlugin()->database(); $table = $database->getTableName(Database::CONST_TABLE_ACCOUNTS); $found = null; @@ -69,9 +72,6 @@ public function createAccountConnection(WP_User $wpUser, string $connection): vo $found = $database->selectRow('*', $table, 'WHERE `user` = %d AND `site` = %d AND `blog` = %d AND `auth0` = "%s" LIMIT 1', [$wpUser->ID, $network, $blog, $connection]); if (null === $found) { - set_transient($cacheKey, $wpUser->ID, 120); - wp_cache_set($cacheKey, $found, 120); - $database->insertRow($table, [ 'user' => $wpUser->ID, 'site' => $network, @@ -100,7 +100,11 @@ public function deleteAccountConnections(int $userId): ?array if ($connections) { $database->deleteRow($table, ['user' => $userId, 'site' => $network, 'blog' => $blog], ['%d', '%s', '%s']); - wp_cache_flush(); + foreach ($connections as $connection) { + $cacheKey = 'auth0_account_' . hash('sha256', $connection . '::' . $network . '!' . $blog); + delete_transient($cacheKey); + wp_cache_delete($cacheKey); + } return $connections; } @@ -399,7 +403,18 @@ public function onInit(): void $sub = $session->user['sub'] ?? null; if (null !== $sub) { - $match = $this->getAccountByConnection($sub); + $sub = sanitize_text_field($session->user['sub'] ?? ''); + $email = sanitize_text_field($session->user['email'] ?? ''); + $verified = $session->user['email_verified'] ?? null; + $match = $this->resolveIdentity(sub: $sub, email: $email, verified: $verified); + + // Create missing account record, can be missing when resuming a session + // or registering on auth0 for an existing WP user. + if (! $match instanceof WP_User && $email === $wordpress->user_email && $verified) { + $this->createAccountConnection($wordpress, $sub); + + return; + } if (! $match instanceof WP_User || $match->ID !== $wordpress->ID) { $this->getSdk()->clear();