From 0dfe1d8afafc59c4a5559c498342668d5a58d6ef Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 23 Jul 2026 22:41:41 +0000
Subject: [PATCH] =Still working away at the Integrations overhaul

---
 inc/rest/routes/LoginRoutes.php |   96 +++++++++++++++++++++++------------------------
 1 files changed, 47 insertions(+), 49 deletions(-)

diff --git a/inc/rest/routes/LoginRoutes.php b/inc/rest/routes/LoginRoutes.php
index ef66bb2..5d8218b 100644
--- a/inc/rest/routes/LoginRoutes.php
+++ b/inc/rest/routes/LoginRoutes.php
@@ -1,9 +1,11 @@
 <?php
 namespace JVBase\rest\routes;
 
+use JVBase\managers\Cache;
+use JVBase\registrar\Registrar;
 use JVBase\rest\Rest;
 use JVBase\rest\Route;
-use JVBase\utility\Features;
+use JVBase\base\Site;
 use WP_REST_Request;
 use WP_REST_Response;
 use WP_Error;
@@ -29,8 +31,8 @@
 		$this->cacheTtl = WEEK_IN_SECONDS;
 
 		parent::__construct();
+		$this->hasMagicLink = Site::has('magic_link');
 
-		$this->hasMagicLink = Features::forSite()->has('magicLink');
 	}
 
 	public function registerRoutes(): void
@@ -39,7 +41,8 @@
 		Route::for('auth/status')
 			->get([$this, 'getAuthStatus'])
 			->auth('public')
-			->rateLimit(60, 60);
+			->rateLimit()
+			->register();
 
 		// Standard login
 		Route::for('auth/login')
@@ -51,7 +54,8 @@
 				'redirect_to' => 'string',
 			])
 			->auth('public')
-			->rateLimit(5, 300);
+			->rateLimit(5, 300)
+			->register();
 
 		// User registration
 		Route::for('auth/register')
@@ -64,7 +68,8 @@
 				'redirect_to' => 'string',
 			])
 			->auth('public')
-			->rateLimit(3, 3600);
+			->rateLimit(3, 3600)
+			->register();
 
 		// Request password reset
 		Route::for('auth/lostpassword')
@@ -73,7 +78,8 @@
 				'user_email' => 'email|required',
 			])
 			->auth('public')
-			->rateLimit(3, 3600);
+			->rateLimit(3, 3600)
+			->register();
 
 		// Reset password with token
 		Route::for('auth/resetpass')
@@ -85,10 +91,12 @@
 				'pass2' => 'string|required',
 			])
 			->auth('public')
-			->rateLimit(5, 300);
+			->rateLimit(5, 300)
+			->register();
 
 		// Magic link endpoint
-		if ($this->hasMagicLink) {
+
+		if (Site::has('magic_link')) {
 			Route::for('auth/magic')
 				->post([$this, 'handleMagicLink'])
 				->args([
@@ -97,14 +105,16 @@
 					'redirect_to' => 'string',
 				])
 				->auth('public')
-				->rateLimit(5, 3600);
+				->rateLimit(5, 3600)
+				->register();
 		}
 
 		// Logout endpoint
 		Route::for('auth/logout')
 			->post([$this, 'handleLogout'])
 			->auth('logged_in')
-			->rateLimit(10, 60);
+			->rateLimit(10)
+			->register();
 	}
 
 	/**
@@ -127,18 +137,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
@@ -254,7 +269,7 @@
 		]);
 
 		// Process referral code if provided
-		if (!empty($referral_code) && Features::forSite()->has('referrals')) {
+		if (!empty($referral_code) && Site::has('referrals')) {
 			$this->processReferralCode($user_id, $referral_code);
 		}
 
@@ -333,7 +348,7 @@
 					'error' => $key->get_error_message(),
 				]);
 			} else {
-				$this->sendPasswordResetEmail($user, $key);
+				$success = JVB()->email()->sendPasswordResetEmail($user, $key);
 			}
 		}
 
@@ -407,7 +422,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',
@@ -623,17 +639,17 @@
 		}
 
 		// Check if role is valid and can register
-		$role_config = JVB_USER[$user_select] ?? null;
+		$registrar = Registrar::getInstance($user_select);
 
-		if (!$role_config) {
+		if (!$registrar) {
 			return new WP_Error('invalid_role', 'Invalid role selected.');
 		}
 
-		if (!($role_config['can_register'] ?? false)) {
+		if (!($registrar->hasFeature('can_register') ?? false)) {
 			return new WP_Error('role_not_allowed', 'This role cannot be selected during registration.');
 		}
 
-		return BASE . $user_select;
+		return $registrar->getBased();
 	}
 
 	/**
@@ -641,7 +657,7 @@
 	 */
 	protected function processReferralCode(int $user_id, string $referral_code): void
 	{
-		if (!Features::forSite()->has('referrals')) {
+		if (!Site::has('referrals')) {
 			return;
 		}
 
@@ -703,61 +719,43 @@
 		}
 	}
 
-	/**
-	 * Send password reset email (fallback if magic links not available)
-	 */
-	protected function sendPasswordResetEmail(WP_User $user, string $key): bool
+	public static function auth():array
 	{
-		$reset_url = network_site_url(
-			"wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login),
-			'login'
-		);
-
-		$subject = 'Password Reset Request';
-		$message = sprintf(
-			"Hello %s,\n\nYou requested a password reset. Click the link below to reset your password:\n\n%s\n\nIf you didn't request this, please ignore this email.",
-			$user->display_name,
-			$reset_url
-		);
-
-		return wp_mail($user->user_email, $subject, $message);
+		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 {
 		$nonces = [
 			'wp_rest'	=> wp_create_nonce('wp_rest'),
 		];
-		if (Features::forSite()->has('dashboard')) {
+		if (Site::has('dashboard')) {
 			$nonces['dash'] = wp_create_nonce('dash-'.$userID);
 		}
-		if (Features::forSite()->has('favourites')) {
+		if (Site::has('favourites')) {
 			$nonces['favourites'] = wp_create_nonce('favourites-'.$userID);
 		}
-		if (Features::anyContentHas('karma') ||
-			Features::anyTaxonomyHas('karma') ||
-			Features::anyUserHas('karma')) {
+		if (!empty(Registrar::withFeature('karma'))) {
 			$nonces['votes'] = wp_create_nonce('votes-'.$userID);
 		}
-		if (Features::forSite()->has('notifications')) {
+		if (Site::has('notifications')) {
 			$nonces['notifications'] = wp_create_nonce('notifications-'.$userID);
 		}
 		return $nonces;

--
Gitblit v1.10.0