如何从WordPress解析Node Express会话

There are :

  • WordPress example.domain
  • Node.js Express with Passport.js example.domain/node

Node.js puts connect.sid to cookies

I can verify a user auth through res.isAuthenticated when user call any method from Node.js

But how to verify a user auth through WordPress?

WordPress renders pages as MVC client

Node.js works as REST API

I've a few ideas:

  • May be create a empty method at Node.js that returns flag and add call of it API method at elements when they are loaded?

  • May be move WP under Node.js return method?

User: I want to example.domain 

Node: Yes, you're authorized and can get WP pages with true-auth flag

User: I want to logout from example.domain

Node: Yes, you're not authorized and can get WP pages with false-auth flag```

If WP and Node.js were placed at one domain, then Cookies contains at one place

You can get Cookies through $_COOKIE["COOKIE NAME HERE"] at WP

Passport.js from Node.js contains session_id as cookies with name connect_sid

When you call any page from WP, you can receive at cookies the connect_sid and validate it on Node.js side through the session substitution

Node (create a route as get):

router.get("/check", function (req, res) {
  res.send(req.isAuthenticated)
})

WP:

function get_auth_state(){
    $cookie = 'Cookie: connect.sid=' . urlencode($_COOKIE["connect_sid"]) . ';';

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "http://localhost-node-address/check",
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            $cookie
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        return "cURL Error #:" . $err;
    } else {
        return $response;
    }
}