Skip to content

Commit 46932f7

Browse files
committed
📦 NEW: Login without domain mapping
1 parent 149f9a0 commit 46932f7

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

‎app/Run.php‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,46 @@ public function deactivate() {
404404

405405
public function handle_auto_login() {
406406
global $pagenow;
407+
408+
// 1. Handle Context Switch
409+
if ( isset( $_GET['freighter_switch'] ) ) {
410+
$site_id = (int) $_GET['freighter_switch'];
411+
$user_id = (int) $_GET['freighter_user'];
412+
$token = $_GET['freighter_token'];
413+
414+
// Set Cookie
415+
$cookie_path = defined( 'SITECOOKIEPATH' ) ? SITECOOKIEPATH : '/';
416+
$cookie_domain = defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : '';
417+
setcookie( 'stacked_site_id', $site_id, time() + 31536000, $cookie_path, $cookie_domain );
418+
419+
// [FIXED] Use site_url() instead of admin_url()
420+
// This prevents generating /wp-admin/wp-login.php
421+
$login_url = add_query_arg([
422+
'user_id' => $user_id,
423+
'captaincore_login_token' => $token,
424+
'redirect_to' => isset($_GET['redirect_to']) ? $_GET['redirect_to'] : admin_url()
425+
], site_url( 'wp-login.php' ) );
426+
427+
nocache_headers();
428+
wp_safe_redirect( $login_url );
429+
exit;
430+
}
431+
432+
// 2. Standard Token Verification
407433
if ( 'wp-login.php' !== $pagenow || empty( $_GET['user_id'] ) || empty( $_GET['captaincore_login_token'] ) ) {
408434
return;
409435
}
410436

411437
$user = get_user_by( 'id', (int) $_GET['user_id'] );
438+
412439
if ( ! $user ) {
413440
wp_die( 'Invalid User' );
414441
}
415442

416443
$token = get_user_meta( $user->ID, 'captaincore_login_token', true );
444+
417445
if ( ! hash_equals( $token, $_GET['captaincore_login_token'] ) ) {
418-
wp_die( 'Invalid Token' );
446+
wp_die( 'Invalid one-time login token.' );
419447
}
420448

421449
delete_user_meta( $user->ID, 'captaincore_login_token' );

‎app/Site.php‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,24 +289,23 @@ public static function update( $site_id, $args ) {
289289
*/
290290
public static function login( $site_id, $redirect_to = '' ) {
291291
global $wpdb;
292-
293-
// 1. Determine Prefix
292+
293+
// 1. Determine Target DB Prefix
294294
if ( 'main' === $site_id ) {
295-
$prefix = self::get_primary_prefix();
295+
$target_prefix = self::get_primary_prefix();
296296
} else {
297297
$site_id = (int) $site_id;
298-
$prefix = "stacked_{$site_id}_";
298+
$target_prefix = "stacked_{$site_id}_";
299299
}
300300

301-
$meta_table = $prefix . "usermeta";
302-
$cap_key = $prefix . "capabilities";
301+
$meta_table = $target_prefix . "usermeta";
302+
$cap_key = $target_prefix . "capabilities";
303303

304-
// 2. Find an Admin user
304+
// 2. Find Admin (IN TARGET DB)
305305
$user_id = $wpdb->get_var( "SELECT user_id FROM $meta_table WHERE meta_key = '$cap_key' AND meta_value LIKE '%administrator%' LIMIT 1" );
306-
307306
if ( ! $user_id ) return new \WP_Error( 'no_admin', 'No administrator found.' );
308307

309-
// 3. Set Token
308+
// 3. Generate Token (IN TARGET DB)
310309
$token = sha1( wp_generate_password() );
311310
$existing = $wpdb->get_var( $wpdb->prepare( "SELECT umeta_id FROM $meta_table WHERE user_id = %d AND meta_key = 'captaincore_login_token'", $user_id ) );
312311

@@ -316,21 +315,41 @@ public static function login( $site_id, $redirect_to = '' ) {
316315
$wpdb->query( $wpdb->prepare( "INSERT INTO $meta_table (user_id, meta_key, meta_value) VALUES (%d, 'captaincore_login_token', %s)", $user_id, $token ) );
317316
}
318317

319-
// 4. Ensure Helper
318+
// 4. Ensure Helper Plugin Exists
320319
if ( 'main' !== $site_id ) {
321320
self::ensure_helper_plugin( $site_id );
322321
}
323322

324323
// 5. Build URL
325-
$site_url = $wpdb->get_var( "SELECT option_value FROM {$prefix}options WHERE option_name = 'siteurl'" );
324+
$configurations = ( new Configurations )->get();
325+
$domain_mapping_on = ( isset($configurations->domain_mapping) && $configurations->domain_mapping === 'on' );
326+
327+
// Get Base URL
328+
if ( 'main' === $site_id || ! $domain_mapping_on ) {
329+
$url_prefix = self::get_primary_prefix();
330+
} else {
331+
$url_prefix = $target_prefix;
332+
}
333+
334+
$site_url = $wpdb->get_var( "SELECT option_value FROM {$url_prefix}options WHERE option_name = 'siteurl'" );
326335
$site_url = rtrim( $site_url, '/' );
327336

328-
$query_args = [
329-
'user_id' => $user_id,
330-
'captaincore_login_token' => $token
331-
];
337+
// [CRITICAL CHANGE] Logic to bypass Helper Plugin on first hit
338+
if ( 'main' !== $site_id && ! $domain_mapping_on ) {
339+
// Use special params that the Helper Plugin DOES NOT recognize
340+
$query_args = [
341+
'freighter_switch' => $site_id,
342+
'freighter_user' => $user_id,
343+
'freighter_token' => $token
344+
];
345+
} else {
346+
// Standard Params for direct login
347+
$query_args = [
348+
'user_id' => $user_id,
349+
'captaincore_login_token' => $token
350+
];
351+
}
332352

333-
// Append redirect_to if provided
334353
if ( ! empty( $redirect_to ) ) {
335354
$query_args['redirect_to'] = $redirect_to;
336355
}

0 commit comments

Comments
 (0)