• Resolved juanguillermo

    (@juanguillermo)


    Hello! I am developing a web application that uses WordPress as a backend (Headless). That is, I consume the WordPress resources and database from the app created in Vue with Node.js through the Rest API.

    User creation is simple and I have it figured out. What I’m having problems with is the login.

    Apparently (and according to WordPress) SWPM is blocking the login.

    More details now:

    1. I send my app username and password to WordPress by Rest API using node-fetch (GET method).
    2. In WordPress I have an Endpoint inside a Custom Plugin (code below).
    3. This endpoint retrieves the URI parameters and sends them to wp_signon to sign in.
    4. If wp_signon succeeds in logging in, it returns the user’s data. If not, it returns an error array.

    This is the custom endpoint en WP:

    // Register REST API endpoints
    class Login_REST_API_Endpoints {
    
        /**
         * Register the routes for the objects of the controller.
         */
        public static function register_endpoints() {
            // endpoints will be registered here
            register_rest_route( 'wp', '/login', array(
                'methods' => 'GET',
                'callback' => array( 'Login_REST_API_Endpoints', 'login' ),
    			'permission_callback' => '__return_true'
            ) );
        }
    
        /**
         * @param WP_REST_Request $request Full data about the request.
         * @return WP_Error|WP_REST_Request
         */
    	public static function login( $request ) {
    
    
    		$data = array();
    		$data['user_login'] = $request["email"];
    		$data['user_password'] =  $request["password"];
    		$data['remember'] = true;
    		$user = wp_signon( $data, false );
    
    		if ( !is_wp_error($user) ){
    			return $user;
    		} else {
    			return $error = json_encode(array('error' => true));
    		}
    
    	}
    }
    add_action( 'rest_api_init', array( 'Login_REST_API_Endpoints', 'register_endpoints' ) );

    So far so good. The problem appears when I activate the SWPM plugin.

    I start getting this response (the asterisks are intentional, to hide the real info):

    {
    	"code": "wp_die",
    	"message": "<p>Warning! The Simple Membership plugin cannot process this access request to prevent you from accidentally logging out as WP admin.<\/p><p><a href=\"https:\/\/*********\/wp-admin\/profile.php\" target=\"_blank\">Click here<\/a>to see the profile with which you are logged in in this browser.<\/p><p>In this browser you have connected to the site as an administrator user. First, log out as WP admin and then you will be able to log in as a member.<\/p><p>Alternatively, you can use a different browser (where you are not logged in as an administrator) to test membership access.<\/p><p>Your frequent visitors or members will never see this message. This message is ONLY for the admin user.<\/p>",
    	"data": {
    		"status": 500
    	},
    	"additional_errors": []
    }

    And these are the headers when the error happens (the asterisks are intentional, to hide the real info):

    Date: Sat, 11 Feb 2023 02:58:36 GMT
    Server: Apache
    Set-Cookie: swpm_session=52883ad4e8ad887e7***************; path=/
    X-Robots-Tag: noindex
    Link: <https://******/wp-json/>; rel="https://api.w.org/"
    X-Content-Type-Options: nosniff
    Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages, Link
    Access-Control-Allow-Headers: Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type
    Expires: Wed, 11 Jan 1984 05:00:00 GMT
    Cache-Control: no-cache, must-revalidate, max-age=0
    Content-Length: 890
    Connection: close
    Content-Type: application/json; charset=UTF-8

    I already tried:

    • Close all sessions.
    • Delete cookies and cache.
    • Deactivate plugin and activate them one by one.
    • Test with different browsers.
    • Try with Insomnia.

    And nothing works.

    The only thing I have noticed is that when SWPM is disabled, the request is successful (code 200).

    Could you please give me some idea of what might be going on and how I could fix it?

    In use:

    • SWPM 4.2.4.
    • WP 6.1.1.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter juanguillermo

    (@juanguillermo)

    Update:
    When I GET request from Insomnia, if the SWPM plugin is active, I get a 500 error in the response body. But if it’s inactive, I get the 200 code, user data in the body, and WP login cookies in the header.

    Insomnia’s settings say that it automatically stores these cookies and sends them when they are needed.

    Now (the interesting thing) if I reactivate the SWPM plugin and do GET, I get code 200, the user data and NEW WP login cookies + SWPM login cookies.

    From then on, all the requests you make to WP will be code 200. Logically, in each login request the cookies change.

    Now, if I copy these cookies to my fetch code in Node.js, I also get 200 code from WP, even if the SWPM plugin is enabled.

    Logically, these cookies expire and it is not something that can be used in production, but this information may help to solve the problem.

    Plugin Support mbrsolution

    (@mbrsolution)

    Thread Starter juanguillermo

    (@juanguillermo)

    Hello!

    Yep, in fact, I am creating the user using the SWPM API.

    However the documentation doesn’t say anything about Login.

    Do you have an API for login?

    What part of the plugin is blocking login through the WordPress API?

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘SWPM cannot process this access request’ is closed to new replies.