Soporte » WordPress Avanzado » Login without password using wp_set_auth_cookie()

  • Hello, I’m trying to login some users using a function found in docs.

    The function does the login and all works great on the frontend, the problem is in the backend, when trying to access it asks for username and password (wp-login.php)

    Is like when trying to access the backend the cookie is lost and WP asks to login again.

    Function I’m using:

    
    function login_external_user($username) {
    
        $user = get_user_by('login', $username );
    
        // If no error received, set the WP Cookie
        if ( !is_wp_error( $user ) )
        {
            wp_clear_auth_cookie();
            wp_set_current_user ( $user->ID ); // Set the current user detail
            wp_set_auth_cookie  ( $user->ID, true ); // Set auth details in cookie
            $message = "Logged in successfully";
            exit;
        } else {
            $message = "Failed to log in";
        }
    
        echo $message;
    }
    

    I need to say also that this code runs on a custom endpoint as it receives data from another website. When data is received on domain.com/login_sso/data?param=data

    
    add_action( 'init', function(){
        add_rewrite_endpoint( 'login_sso', EP_ROOT );
    } );
    
    add_action( 'template_redirect', function() {
        if ( $login_sso_Url = get_query_var( 'login_sso' ) ) {
    
            //do stuff
    
            // then stop processing
            die();
        }
    });
    

    Any ideas?

Viendo 1 respuesta (de un total de 1)
  • Iniciador del debate Daniel Pereyra Costas

    (@danidub)

    I have found that when using wp_set_auth_cookie inside a function in functions.php does set the cookies but not the wordpress_sec_ cookie correctly… So when entering on the backend the system fails and need to login again.

    Any ideas?

Viendo 1 respuesta (de un total de 1)
  • Debes estar registrado para responder a este debate.