【发布时间】:2020-11-25 01:47:01
【问题描述】:
我正在构建一个身份验证微服务;客户端在codesandbox上,服务器端在repl上
我正在尝试将访问令牌存储在服务器端(仅限 http)
我已经设置了 cookie 解析器
//COOKIE
const cookieParser = require('cookie-parser');
app.use(cookieParser());
我这样发送cookie
res.cookie("access_token", access_token,cookieOptions)
.status(200)
.send({success: true,token_type:"bearer",
expires_in: new Date(Date.now() + 60 * 1000 ),
refresh_token: refresh_token})
这是客户端
function fetch_profile_accountInfo(username, password) {
let url = "https://repl.co/api/auth/localLogin";
return axios
.post(url, {
username: username.toLowerCase(),
password: password
})
.then((res) => res.data);
}
}
function getCookie(cname) {
var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === " ") {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
let { refresh_token, expires_in } = await fetch_profile_accountInfo(
username,
password
}
const access_token = getCookie("access_token");
console.log("document: " + document.cookie);
console.log("access_token: " + access_token);
当我调用 document.cookie 时,我得到 null 或 undefined
我也试过设置 axios 使用 withCredentials: true
function fetch_profile_accountInfo(username, password) {
let url = "https://repl.co/api/auth/localLogin";
return axios
.post(url, {
username: username.toLowerCase(),
password: password
}, {withCredentials: true} )
.then((res) => res.data);
}
但我遇到网络错误
有谁知道如何解决这个问题以从 express 获取 cookie?
另外我也想问一下,把access token存放在server cookie上,refresh token存放在client端localstorage上好不好?
谢谢
【问题讨论】: