I am building an auth microservice; the client side is on codesandbox and server side is on repl
I am trying to store the access token on server side (http only)
I already setup cookie parser
//COOKIE
const cookieParser = require('cookie-parser');
app.use(cookieParser());
I send the cookie like this
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})
And this is client side
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);
When I call the document.cookie, I am getting null or undefined
I also tried set the axios use 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);
}
But I am getting network error
Anyone know how to fix this to get cookie from express?
In addition, I also want to ask about it is good to store access token on server cookie and refresh token on client side localstorage?
Thanks