1. const authCache = {};
  2. /**
  3. * Retrieves an authentication token from the cache.
  4. * @param {string} token - The authentication token to retrieve.
  5. * @returns {string | undefined} - The token if found, otherwise undefined.
  6. */
  7. function getAuthToken(token) {
  8. if (authCache.hasOwnProperty(token)) {
  9. return authCache[token];
  10. }
  11. return undefined;
  12. }
  13. /**
  14. * Stores an authentication token in the cache.
  15. * @param {string} token - The authentication token to store.
  16. * @param {any} data - Optional data associated with the token.
  17. */
  18. function setAuthToken(token, data) {
  19. authCache[token] = data;
  20. }
  21. /**
  22. * Removes an authentication token from the cache.
  23. * @param {string} token - The authentication token to remove.
  24. */
  25. function removeAuthToken(token) {
  26. delete authCache[token];
  27. }
  28. //Example Usage (for testing)
  29. //setAuthToken("testToken", {user: "testUser"});
  30. //const token = getAuthToken("testToken");
  31. //console.log(token); //Output: {user: "testUser"}
  32. //removeAuthToken("testToken");
  33. //const token2 = getAuthToken("testToken");
  34. //console.log(token2); //Output: undefined

Add your comment