Skip to content

Commit

Permalink
added check for type of object
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavanakarwade committed Dec 5, 2024
1 parent aaa15c3 commit e27b1c1
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/api/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,30 @@ export const encryptData = (value: any): string => {
}
}

export const decryptData = (value: any): string => {
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`
export const decryptData = (value: string): string => {
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`;

try {
let bytes = CryptoJS.AES.decrypt(value, CRYPTO_PRIVATE_KEY);
return bytes.toString(CryptoJS.enc.Utf8);
// Ensure input is valid and not empty
if (!value || typeof value !== "string") {
throw new Error("Invalid input for decryption");
}

const bytes = CryptoJS.AES.decrypt(value, CRYPTO_PRIVATE_KEY);
const decryptedText = bytes.toString(CryptoJS.enc.Utf8);

// Ensure the output is valid UTF-8
if (!decryptedText) {
throw new Error("Decryption failed or returned empty result");
}

return decryptedText;
} catch (error) {
// Handle decryption error or invalid input
console.error('Decryption error:', error);
return '';
console.error("Decryption error:", error);
return ''; // Return a fallback value to prevent crashes
}
}
};


export const setToLocalStorage = async (key: string, value: any) =>{
// If passed value is object then checked empty object
Expand Down

0 comments on commit e27b1c1

Please sign in to comment.