javascript - 如何从 WordPress 解析 Node Express 会话
问题描述
有 :
- WordPress
example.domain
- Node.js Express 与 Passport.js
example.domain/node
Node.js 放入connect.sid
cookie
res.isAuthenticated
当用户从 Node.js 调用任何方法时,我可以验证用户身份验证
但是如何通过 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```
解决方案
如果 WP 和 Node.js 放在一个域中,那么 Cookies 包含在一个位置
您可以通过 WP 的 $_COOKIE["COOKIE NAME HERE"] 获取 Cookie
来自 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)
})
工作人员:
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;
}
}
推荐阅读
- reactjs - 将连接字符串添加到生产应用程序
- android - 无法完成会话:INSTALL_FAILED_INVALID_APK: /data/app/....tmp/...-debug 版本代码 29 与 28 不一致
- powershell - 当我的客户端在 PowerShell 中的域 C 中时,从域 B 中的 AD 组中删除域 A 中的 AD 用户
- c++ - 用户定义的转换顺序
- android - Android模拟器在更新后呈现没有抗锯齿
- php - 如何使用数据库中的数据填充复选框?
- html - 缩小时三角形边框消失,放大时折叠
- selenium - 将 shell 脚本与 selenium 集成
- django - 在 Django admin 中使用 import-export 从父模型中导出内联 ForeignKey 条目
- sql-server - 如何从 SQL Server 数据库中获取数据快照、存储它,然后将其与参考快照进行比较