Skip to content

Commit 1716116

Browse files
committed
[dashboard] Check if localStorage is available before using it
1 parent c2e103f commit 1716116

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

components/dashboard/src/login/SSOLoginForm.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useFeatureFlag } from "../data/featureflag-query";
1414
import { useLocation } from "react-router";
1515
import { useOnboardingState } from "../dedicated-setup/use-needs-setup";
1616
import { getOrgSlugFromQuery } from "../data/organizations/orgs-query";
17+
import { storageAvailable } from "../utils";
1718

1819
type Props = {
1920
onSuccess: () => void;
@@ -34,10 +35,7 @@ export const SSOLoginForm: FC<Props> = ({ onSuccess }) => {
3435
const singleOrgMode = (onboardingState?.organizationCountTotal || 0) < 2;
3536

3637
const [orgSlug, setOrgSlug] = useState(
37-
getOrgSlugFromPath(location.pathname) ||
38-
window.localStorage.getItem("sso-org-slug") ||
39-
getOrgSlugFromQuery(location.search) ||
40-
"",
38+
getOrgSlugFromPath(location.pathname) || readSSOOrgSlug() || getOrgSlugFromQuery(location.search) || "",
4139
);
4240
const [error, setError] = useState("");
4341

@@ -46,7 +44,7 @@ export const SSOLoginForm: FC<Props> = ({ onSuccess }) => {
4644
const openLoginWithSSO = useCallback(
4745
async (e: React.FormEvent<HTMLFormElement>) => {
4846
e.preventDefault();
49-
window.localStorage.setItem("sso-org-slug", orgSlug.trim());
47+
persistSSOOrgSlug(orgSlug.trim());
5048

5149
try {
5250
await openOIDCStartWindow({
@@ -105,3 +103,18 @@ export const SSOLoginForm: FC<Props> = ({ onSuccess }) => {
105103
</form>
106104
);
107105
};
106+
107+
function readSSOOrgSlug(): string | undefined {
108+
const isLocalStorageAvailable = storageAvailable("localStorage");
109+
if (isLocalStorageAvailable) {
110+
return window.localStorage.getItem("sso-org-slug") || undefined;
111+
}
112+
return undefined;
113+
}
114+
115+
function persistSSOOrgSlug(slug: string) {
116+
const isLocalStorageAvailable = storageAvailable("localStorage");
117+
if (isLocalStorageAvailable) {
118+
window.localStorage.setItem("sso-org-slug", slug.trim());
119+
}
120+
}

0 commit comments

Comments
 (0)