So everytime I start my nodejs Server I set the userId:
var customerId = null;
When the user tries to log in I set the cookie :
customerId = myCart.Login(FormData.username, FormData.password);
_setCookie(res, "customerId=" + customerId);
Set cookie:
function _setCookie(res, cookieStr) {
res.setHeader('set-cookie', cookieStr);
}
The problem is when I close to application and I restart it the cookie is still setted but the customerId is setted to null. This causes the error when I try to logg off the user:
if (userIsLogedIn(req, "customerId")) {
_processFormData(req, res,
(req, res, FormData) => {
myCart.LogOffCustomer(customerId);
_delCookie(res, "customerId=" + customerId);
myWebServer.redirect(res, "Success.html");
},
(res) => {
myWebServer.redirect(res, "LoggOffFailed.html");
}
);
}
else {
myWebServer.redirect(res, "NotLoggedIn.html");
}
Here I check if the user is logged in:
function userIsLogedIn(httpMessage, cookieName) {
if (httpMessage.headers.cookie == undefined) {
return false;
}
var cookies = _parseCookies(httpMessage.headers.cookie);
return cookies[cookieName] != undefined;
}
function _parseCookies(cookieName) {
var cookies = {};
cookieName.split(';').forEach(function (cookie) {
var parts = cookie.match(/(.*?)=(.*)$/)
cookies[parts[1].trim()] = (parts[2] || '').trim();
});
return cookies;
};
So when I try to logg off the user I check if the cookie is setted. The cookie is only setted when the user is loged in. But when I close the server and restart him the userId is null and so I cant logg off the user.It means that I can only logg off the user if I don't close the server. But I wan't it than even when I reset the server I still can logg off the user.
via igodie
No comments:
Post a Comment