Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: sea-snake <[email protected]>
  • Loading branch information
lmuntaner and sea-snake committed Dec 6, 2024
1 parent aa5a226 commit c453944
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/frontend/src/utils/findWebAuthnRpId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DeviceData } from "$generated/internet_identity_types";

const defaultDomain = "https://identity.ic0.app";
const DEFAULT_DOMAIN = "https://identity.ic0.app";

/**
* Helper to extract the top and secondary level domain from a URL.
Expand All @@ -14,13 +14,13 @@ const defaultDomain = "https://identity.ic0.app";
* @throws {Error} If the URL does not contain a top and secondary level domain.
*/
const getTopAndSecondaryLevelDomain = (url: string): string => {
const parts = new URL(url).hostname.split(".").reverse();
const parts = new URL(url).hostname.split(".");

if (parts.length < 2) {
throw new Error("Invalid URL: Unable to extract domain.");
}

return `${parts[1]}.${parts[0]}`;
return parts.slice(-2).join('.');
};

/**
Expand All @@ -37,7 +37,7 @@ const getDevicesForDomain = (
): DeviceData[] =>
devices.filter((d) => {
if (d.origin.length === 0)
return domain === getTopAndSecondaryLevelDomain(defaultDomain);
return domain === getTopAndSecondaryLevelDomain(DEFAULT_DOMAIN);
return d.origin.some((o) => getTopAndSecondaryLevelDomain(o) === domain);
});

Expand Down Expand Up @@ -81,7 +81,7 @@ export const findWebAuthnRpId = (
"Not possible. Call this function only if devices exist."
);
}
return devices[0].origin[0] ?? defaultDomain;
return devices[0].origin[0] ?? DEFAULT_DOMAIN;
};

// Try current domain first if devices exist
Expand Down

0 comments on commit c453944

Please sign in to comment.