Jake Vanderwerf
2026-08-25 3d25ad7de4fdbb3eba2cde2a62422e6be5d79a0a
inc/rest/routes/LoginRoutes.php
@@ -1,6 +1,9 @@
<?php
namespace JVBase\rest\routes;
use JVBase\Instance;
use JVBase\managers\Cache;
use JVBase\managers\Emails;
use JVBase\registrar\Registrar;
use JVBase\rest\Rest;
use JVBase\rest\Route;
@@ -21,6 +24,7 @@
 */
class LoginRoutes extends Rest
{
   use Instance;
   protected ?string $requestId = null;
   protected bool $hasMagicLink = false;
@@ -30,8 +34,8 @@
      $this->cacheTtl = WEEK_IN_SECONDS;
      parent::__construct();
      $this->hasMagicLink = Site::has('magic_link');
      $this->hasMagicLink = Site::has('magicLink');
   }
   public function registerRoutes(): void
@@ -94,7 +98,8 @@
         ->register();
      // Magic link endpoint
      if ($this->hasMagicLink) {
      if (Site::has('magic_link')) {
         Route::for('auth/magic')
            ->post([$this, 'handleMagicLink'])
            ->args([
@@ -135,18 +140,23 @@
    */
   public function handleLogin(WP_REST_Request $request): WP_REST_Response
   {
      error_log('Handling login...');
      $email = sanitize_email($request->get_param('user_email'));
      $password = $request->get_param('user_password');
      $remember = (bool) $request->get_param('remember_me');
      $redirect_to = $request->get_param('redirect_to');
      // Verify Turnstile
      if (!$this->verifyTurnstile($request->get_param('cf-turnstile-response') ?? '')) {
         error_log('[handleLogin]Turnstile failed');
         return $this->error(
            'Security verification failed. Please try again.',
            'turnstile_failed',
            403
         );
      } else {
         error_log('[handleLogin]Turnstile succeeded');
      }
      // Attempt authentication
@@ -273,7 +283,7 @@
      // Send magic link for email verification
      if ($this->hasMagicLink) {
         JVB()->magicLink()->sendMagicLink($email, 'signup', [
         MagicLinks::getInstance()->sendMagicLink($email, 'signup', [
            'name' => $name,
            'role' => $role,
         ]);
@@ -324,7 +334,7 @@
      // Use magic link if available, otherwise standard WP reset
      if ($this->hasMagicLink) {
         $result = JVB()->magicLink()->sendMagicLink($email, 'reset');
         $result = MagicLinks::getInstance()->sendMagicLink($email, 'reset');
         if (is_wp_error($result)) {
            $this->logError('Magic link send failed', [
@@ -341,7 +351,7 @@
               'error' => $key->get_error_message(),
            ]);
         } else {
            $success = JVB()->email()->sendPasswordResetEmail($user, $key);
            $success = Emails::getInstance()->sendPasswordResetEmail($user, $key);
         }
      }
@@ -415,7 +425,8 @@
    */
   public function handleMagicLink(WP_REST_Request $request): WP_REST_Response
   {
      if (!$this->hasMagicLink) {
      if (!Site::has('magic_link')) {
         return $this->error(
            'Magic link authentication is not enabled.',
            'feature_disabled',
@@ -442,7 +453,7 @@
      }
      // Send magic link
      $result = JVB()->magicLink()->sendMagicLink($email, $type, $context);
      $result = MagicLinks::getInstance()->sendMagicLink($email, $type, $context);
      if (is_wp_error($result)) {
         $this->logError('Magic link send failed', [
@@ -654,7 +665,7 @@
      }
      try {
         JVB()->referrals()->processReferralCode($user_id, $referral_code);
         Referrals::getInstance()->processReferralCode($user_id, $referral_code);
      } catch (\Exception $e) {
         $this->logError('Referral processing failed', [
            'user_id' => $user_id,
@@ -711,23 +722,27 @@
      }
   }
   public static function auth():array
   {
      return (new self)->buildAuth();
   }
   protected function buildAuth(?int $user = null): array
   {
      if (is_user_logged_in()) {
         $user = ($user) ?: get_current_user_id();
      $userId = $user ?? (is_user_logged_in() ? get_current_user_id() : 0);
      if ($userId) {
         return [
            'authenticated' => true,
            'user' => $user,
            'nonces' => $this->getUserNonces($user)
            'user'          => $userId,
            'nonces'        => $this->getUserNonces($userId),
         ];
      }
      return [
         'authenticated' => false,
         'user' => false,
         'nonces' => [
            'wp_rest' => wp_create_nonce('wp_rest')
         ]
         'user'          => false,
         'nonces'        => ['wp_rest' => wp_create_nonce('wp_rest')],
      ];
   }
   protected function getUserNonces(int $userID):array {
@@ -740,7 +755,7 @@
      if (Site::has('favourites')) {
         $nonces['favourites'] = wp_create_nonce('favourites-'.$userID);
      }
      if (!empty(Registrar::getFeatured('karma'))) {
      if (!empty(Registrar::withFeature('karma'))) {
         $nonces['votes'] = wp_create_nonce('votes-'.$userID);
      }
      if (Site::has('notifications')) {