如何从WordPress解析Node Express会话

时间:2019-06-14 10:52:11

标签: javascript php node.js wordpress

有:

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

Node.js将connect.sid放入Cookie

当用户从Node.js调用任何方法时,我可以通过res.isAuthenticated验证用户的身份

但是如何通过WordPress验证用户身份验证?

WordPress将页面呈现为MVC客户端

Node.js用作REST API

我有一些想法:

  • 是否可以在Node.js上创建一个空方法,该方法返回标志并在元素加载时在元素上添加对API方法的调用?

  • 可以在Node.js返回方法下移动WP吗?

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```

1 个答案:

答案 0 :(得分:0)

如果将WP和Node.js放在一个域中,则Cookies包含在一个地方

您可以通过WP的$ _COOKIE [“ COOKIE NAME HERE”]获取Cookies

Node.js中的Passport.js包含session_id作为名为connect_sid的cookie

当您从WP调用任何页面时,您可以在cookie处接收connect_sid并通过会话替换在Node.js端对其进行验证

节点(将路径创建为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;
    }
}