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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器