[wp-trac] [WordPress Trac] #61874: Unable to access WordPress login session during login

WordPress Trac noreply at wordpress.org
Thu Aug 15 07:00:33 UTC 2024


#61874: Unable to access WordPress login session during login
--------------------------+------------------------------
 Reporter:  dd32          |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Users         |     Version:
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by dd32):

 There is a workaround, by watching for a cookie set action:
 {{{#!php
 // The workaround.
 add_action('set_auth_cookie', function( $auth_cookie, $expire,
 $expiration, $user_id, $scheme, $token ) {
         global $session_token;

         $session_token = $token;
 }, 10, 6 );

 add_filter( 'login_redirect', function( $redirect, $orig_redirect, $user )
 {
         global $session_token;

         var_dump( [
                 'variant' => 'The Workaround',
                 '$user->ID' => $user->ID,
                 'get_current_user_id()' => get_current_user_id(),
                 'wp_get_session_token()' => wp_get_session_token(),
                 'session_data' => WP_Session_Tokens::get_instance(
 $user->ID )->get( wp_get_session_token() ),
                 'global $session_token' => $session_token,
                 'session_data (via global token)' =>
 WP_Session_Tokens::get_instance( $user->ID )->get( $session_token )
         ] );
         die();
 }, 20, 3 );
 }}}

 which then does allow access to the session data:
 {{{
 wp-content/mu-plugins/example.php:
 array (size=7)
   'variant' => string 'The Workaround' (length=14)
   '$user->ID' => int 1
   'get_current_user_id()' => int 0
   'wp_get_session_token()' => string '' (length=0)
   'session_data' => null
   'global $session_token' => string
 '8XpzO0LxNDfqeHHaHduytWPFVWKYjnfBounb39FfPhK' (length=43)
   'session_data (via global token)' =>
     array (size=6)
       'foo' => string 'bar' (length=3)
       ....
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61874#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list