Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export {
export { useStore } from "./store";
export type { State } from "./store";
export type { StacItemCollection, StacValue } from "./types/stac";
export { buildAuth } from "./utils/auth";
export type { BuildAuthOptions } from "./utils/auth";
29 changes: 7 additions & 22 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,19 @@ import { createRoot } from "react-dom/client";
import changelog from "../CHANGELOG.md?raw";
import { version } from "../package.json";
import Footer from "./components/footer";
import { StacMap, type StacMapProps } from "./components/stac-map";
import { StacMap } from "./components/stac-map";
import { buildAuth } from "./utils/auth";

const rootStyle = {
height: "100dvh",
width: "100dvw",
} as const;

const authority = import.meta.env.VITE_AUTH_AUTHORITY as string | undefined;
const clientId = import.meta.env.VITE_AUTH_CLIENT_ID as string | undefined;

const auth: StacMapProps["auth"] | undefined =
authority && clientId
? {
authority,
client_id: clientId,
redirect_uri:
window.location.origin +
(import.meta.env.BASE_URL ?? "/").replace(/\/$/, "") +
"/",
onSigninCallback: () => {
window.history.replaceState(
{},
document.title,
window.location.pathname + window.location.search
);
},
}
: undefined;
const auth = buildAuth({
authority: import.meta.env.VITE_AUTH_AUTHORITY as string | undefined,
clientId: import.meta.env.VITE_AUTH_CLIENT_ID as string | undefined,
basePath: import.meta.env.BASE_URL,
});

const defaultHref = import.meta.env.VITE_DEFAULT_HREF as string | undefined;
const stacBrowserUrl = import.meta.env.VITE_STAC_BROWSER_URL as
Expand Down
29 changes: 29 additions & 0 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import type { AuthProviderProps } from "react-oidc-context";
import { useStore } from "../store";

export interface BuildAuthOptions {
authority: string | undefined;
clientId: string | undefined;
basePath?: string;
}

export function buildAuth({
authority,
clientId,
basePath = "/",
}: BuildAuthOptions): AuthProviderProps | undefined {
if (!authority || !clientId) {
return undefined;
}
return {
authority,
client_id: clientId,
redirect_uri: window.location.origin + basePath.replace(/\/$/, "") + "/",
onSigninCallback: () => {
window.history.replaceState(
{},
document.title,
window.location.pathname + window.location.search
);
},
};
}

export function getAccessToken(href?: string | URL): string | null {
const state = useStore.getState();

Expand Down
Loading