mirror of
https://github.com/mudabbir-ahmad/Media-Aggregation-and-Sorting-System.git
synced 2026-10-07 19:40:20 +00:00
created a localised login system
This commit is contained in:
1 parent
59ac1fc926
commit
37511780b7
16 files changed
+3721
-20
No files matched your search
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Credential Encryption Service
|
||||
*
|
||||
* Encrypts sensitive credentials before storing in database
|
||||
* Uses base64 encoding (React Native compatible)
|
||||
* For production, use a proper encryption library like react-native-aes-crypto
|
||||
*/
|
||||
|
||||
class CredentialEncryption {
|
||||
constructor() {
|
||||
// Simple key for demo - in production, use proper key management
|
||||
this.encryptionKey = 'photo-app-secret-key-2026';
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64 encode a string (React Native compatible)
|
||||
* Uses btoa which is available in React Native
|
||||
*/
|
||||
base64Encode(str) {
|
||||
try {
|
||||
// btoa is available in React Native
|
||||
return btoa(unescape(encodeURIComponent(str)));
|
||||
} catch (error) {
|
||||
console.error('Base64 encode error:', error);
|
||||
throw new Error('Failed to encode data');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64 decode a string (React Native compatible)
|
||||
* Uses atob which is available in React Native
|
||||
*/
|
||||
base64Decode(str) {
|
||||
try {
|
||||
// atob is available in React Native
|
||||
return decodeURIComponent(escape(atob(str)));
|
||||
} catch (error) {
|
||||
console.error('Base64 decode error:', error);
|
||||
throw new Error('Failed to decode data');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt credentials before storing
|
||||
* Converts object to JSON string, then base64 encodes it
|
||||
*/
|
||||
encrypt(data) {
|
||||
try {
|
||||
if (!data) {
|
||||
throw new Error('No data provided to encrypt');
|
||||
}
|
||||
const jsonString = JSON.stringify(data);
|
||||
const encoded = this.base64Encode(jsonString);
|
||||
console.log('✓ Credentials encrypted successfully');
|
||||
return encoded;
|
||||
} catch (error) {
|
||||
console.error('Encryption error:', error);
|
||||
throw new Error('Failed to encrypt credentials: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt credentials after retrieving
|
||||
* Base64 decodes, then parses JSON back to object
|
||||
*/
|
||||
decrypt(encryptedData) {
|
||||
try {
|
||||
if (!encryptedData) {
|
||||
return null;
|
||||
}
|
||||
const jsonString = this.base64Decode(encryptedData);
|
||||
const data = JSON.parse(jsonString);
|
||||
console.log('✓ Credentials decrypted successfully');
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('Decryption error:', error);
|
||||
throw new Error('Failed to decrypt credentials: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if data is already encrypted (base64)
|
||||
*/
|
||||
isEncrypted(data) {
|
||||
if (typeof data !== 'string') return false;
|
||||
try {
|
||||
this.base64Decode(data);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new CredentialEncryption();
|
||||
|
||||
Reference in new issue
Block a user