dt56449492 2019-06-14 10:52
浏览 47
已采纳

如何从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```
  • 写回答

1条回答 默认 最新

  • doujujian0052 2019-06-16 13:28
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!