Currently, I am working on developing a single-page application using Durandal framework.
In my project, I have implemented two important functions. The first function is responsible for saving a Bearer token after the user logs in so that it can be used to make API calls later. The second function is designed to retrieve the saved Bearer token when needed from different pages within the app.
To enhance the functionality, I created two more functions - 'setCookies' and 'getCookies'. 'setCookies' should work alongside 'setToken', while 'getCookies' should be synchronized with 'getToken'.
However, I encountered an issue where my browser displayed an error saying 'Uncaught ReferenceError: setCookies is not defined'.
I would appreciate any insights or suggestions on what might be causing this problem. Below is the relevant code snippet:
define(['durandal/app', 'knockout'], function (app, ko) {
return {
bearerToken: ko.observable(),
getToken: function () {
this.bearerToken(getCookies());
return this.bearerToken();
},
setToken: function (token) {
setCookies("Bearer", token);
this.bearerToken(token);
console.error(this.bearerToken());
},
getCookies: function (cookieName) {
var cookieValue = document.cookie.match('(^|;) ?' + cookieName + '=([^;]*)(;|$)');
return cookieValue ? cookieValue[2] : null;
},
setCookies: function (cookieName, cookieValue) {
document.cookie = cookieName + "=" + cookieValue;
}
};
});