Skip to content

Commit bd25b05

Browse files
authored
fix: Updates "globus.embed" fields to prompt for authorization when no session is found (#255)
1 parent 57a5a25 commit bd25b05

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/components/Fields/GlobusEmbedField.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { isAuthorizationRequirementsError } from "@globus/sdk/core/errors";
1919
import { useOAuthStore } from "@/store/oauth";
2020
import { usePathname, useSearchParams } from "next/navigation";
2121
import { ExternalLinkIcon } from "@chakra-ui/icons";
22+
import { useLogin } from "@/hooks/useOAuth";
2223
import { PlotlyRenderer } from "./Renderer/Plotly";
2324
import { ObjectRenderer } from "./Renderer/Object";
2425
import { useGCSAsset, useGCSAssetMetadata } from "@/hooks/useGlobusAPI";
@@ -178,6 +179,9 @@ export default function GlobusEmbedField({ field }: { field: ProcessedField }) {
178179
}
179180

180181
function GlobusEmbed({ config, field }: GlobusEmbedProps) {
182+
const auth = useGlobusAuth();
183+
const login = useLogin();
184+
181185
const metadata = useGCSAssetMetadata({
182186
collection: config.collection,
183187
url: config.asset,
@@ -198,6 +202,19 @@ function GlobusEmbed({ config, field }: GlobusEmbedProps) {
198202
return (
199203
<>
200204
<Box w={`calc(${width} + 2em)`} h={`calc(${height} + 2em)`}>
205+
{!auth.isAuthenticated && (
206+
<Alert status="warning">
207+
<AlertIcon />
208+
<AlertTitle>Authentication Required</AlertTitle>
209+
<AlertDescription>
210+
To view this asset, you must{" "}
211+
<Button variant="link" onClick={login}>
212+
Sign In
213+
</Button>
214+
.
215+
</AlertDescription>
216+
</Alert>
217+
)}
201218
{metadata.isLoading && (
202219
<Center>
203220
<HStack>

src/hooks/useGlobusAPI.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ export function useGCSAssetMetadata({
9393
url: string;
9494
}) {
9595
const auth = useGlobusAuth();
96+
9697
const token =
9798
auth.authorization?.tokens.getByResourceServer(collection)?.access_token;
9899
return useQuery({
99-
enabled: !!token,
100+
enabled: !!auth.isAuthenticated,
100101
queryKey: ["gcs", url, "metadata"],
101102
queryFn: async () => {
102103
const response = await fetch(url, {
@@ -154,7 +155,7 @@ export function useGCSAsset({
154155
auth.authorization?.tokens.getByResourceServer(collection)?.access_token;
155156

156157
return useQuery({
157-
enabled: !!token && enabled !== false,
158+
enabled: !!auth.isAuthenticated && enabled !== false,
158159
queryKey: ["gcs", url],
159160
queryFn: async (): Promise<GCSAssetResponse> => {
160161
let contentType = mimeTypeHint;
@@ -214,7 +215,7 @@ export function useGCSAsset({
214215
});
215216
contentType = response.headers.get("Content-Type");
216217
if (!response.ok) {
217-
throw new Error(
218+
return Promise.reject(
218219
/**
219220
* Errors might come from GridFTP, so we need to detect whether or
220221
* not the response is JSON or text before throwing an error.

0 commit comments

Comments
 (0)