Skip to content

Commit

Permalink
use local storage all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Aug 5, 2024
1 parent 1bdedca commit a55844b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ class CustomAuth {

if (!result)
return {
error: "Init parameters not found. It might be because storage is not available. Please retry the login in a different browser",
error: `Init parameters not found. It might be because storage is not available. Please retry the login in a different browser. Used storage method: ${this.storageHelper.storageMethodUsed}`,
state: instanceParameters || {},
method,
result: {},
Expand Down
39 changes: 22 additions & 17 deletions src/utils/StorageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ class StorageHelper {

private storageServerUrl = "https://session.web3auth.io";

private localStorageAvailable: boolean = true;

constructor(serverUrl: string) {
this.storageServerUrl = serverUrl;
}

get storageMethodUsed(): REDIRECT_PARAMS_STORAGE_METHOD_TYPE {
return this.currentStorageMethod;
}

init() {
const support = are3PCSupported();
const localStorageAvailable = storageAvailable(REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE);
this.localStorageAvailable = localStorageAvailable;
if (support && localStorageAvailable) {
// use local storage as default for storing stuff
this.currentStorageMethod = REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE;
Expand All @@ -40,22 +47,25 @@ class StorageHelper {
const encData = await encryptData(privKeyHex, params);
const signature = (await sign(privKey, keccak256(Buffer.from(encData, "utf8")))).toString("hex");
await post(`${this.storageServerUrl}/store/set`, { key: publicKeyHex, data: encData, signature });
} else {
window.localStorage.setItem(`torus_login_${scope}`, JSON.stringify(params));
}
if (this.localStorageAvailable) window.localStorage.setItem(`torus_login_${scope}`, JSON.stringify(params));
}

async retrieveLoginDetails(scope: string): Promise<LoginDetails> {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.localStorageAvailable) {
const loginDetails = window.localStorage.getItem(`torus_login_${scope}`);
if (loginDetails) return JSON.parse(loginDetails || "{}") as LoginDetails;
}
if (this.currentStorageMethod === REDIRECT_PARAMS_STORAGE_METHOD.SERVER) {
const privKey = keccak256(Buffer.from(scope, "utf8"));
const privKeyHex = privKey.toString("hex");
const publicKeyHex = getPublic(privKey).toString("hex");
try {
const encData: { message: string; success: boolean } = await get(`${this.storageServerUrl}/store/get?key=${publicKeyHex}`);
if (encData.message) {
const loginDetails = await decryptData<LoginDetails>(privKeyHex, encData.message);
return loginDetails;
const currentLoginDetails = await decryptData<LoginDetails>(privKeyHex, encData.message);
return currentLoginDetails;
}
} catch (error) {
if ((error as Response).status === 404) {
Expand All @@ -65,28 +75,23 @@ class StorageHelper {
}
}
}
const loginDetails = window.localStorage.getItem(`torus_login_${scope}`);
return JSON.parse(loginDetails || "{}") as LoginDetails;
}

clearLoginDetailsStorage(scope: string): void {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.currentStorageMethod === REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE) {
window.localStorage.removeItem(`torus_login_${scope}`);
}
if (this.localStorageAvailable) window.localStorage.removeItem(`torus_login_${scope}`);
// No need to clear server details cause they auto expire and scope is never re-used for different login attempts
}

clearOrphanedLoginDetails(): void {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.currentStorageMethod === REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE) {
const allStorageKeys = Object.keys(window.localStorage);
allStorageKeys.forEach((key) => {
if (key.startsWith("torus_login_")) {
window.localStorage.removeItem(key);
}
});
}
if (!this.localStorageAvailable) return;
const allStorageKeys = Object.keys(window.localStorage);
allStorageKeys.forEach((key) => {
if (key.startsWith("torus_login_")) {
window.localStorage.removeItem(key);
}
});
// No need to clear server details cause they auto expire and scope is never re-used for different login attempts
}
}
Expand Down

0 comments on commit a55844b

Please sign in to comment.