@code_district/doorman
Version:
Doorman: A comprehensive React package for seamless authentication and authorization management. Easily integrate secure user authentication and access control in your applications. Streamline user verification, role-based permissions, and secure data han
32 lines (25 loc) • 906 B
JavaScript
import CryptoJS from "crypto-js";
import Cookies from "js-cookie";
import K from "../../utilities/constants";
// Function to retrieve and decrypt the cookie
export const DoormanGetUserFromCookie = () => {
try {
// Retrieve the encrypted cookie
let encryptedUser = Cookies.get(K.Cookie.User);
if (!encryptedUser) {
console.log("No user cookie found");
return null;
}
// Decrypt the cookie
let bytes = CryptoJS.AES.decrypt(encryptedUser, K.Cookie.EncryptionKey);
// Convert the decrypted data to a string
let decryptedUser = bytes.toString(CryptoJS.enc.Utf8);
// Parse the JSON string into an object
let userObject = JSON.parse(decryptedUser);
// Return the user object
return userObject;
} catch (error) {
console.error("Error decrypting user cookie:", error);
return null; // Return null if decryption or retrieval fails
}
};