Skip to content

fix: support authentication in image #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
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
6 changes: 6 additions & 0 deletions src/components/image/image.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export const Default = {
args: {
alt: 'A curved facade covered in white latticework',
src: 'images/image-component-example.png',
getAuthHeaders: () =>
Promise.resolve({
headers: {
Authorization: 'Bearer example-token',
},
}),
},
}

Expand Down
48 changes: 38 additions & 10 deletions src/components/image/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '../../index.css'

import CircularProgress from '@mui/material/CircularProgress'
import Typography from '@mui/material/Typography'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import React from 'react'

import { getSizeStyles } from '../helper'
Expand All @@ -17,6 +17,32 @@ export default function Image({
}: ImageFormat) {
const [isLoading, setIsLoading] = useState(true)
const [errorMessage, setErrorMessage] = useState<string>('')
const [objectUrl, setObjectUrl] = useState<string>('')

useEffect(() => {
if (props.getAuthHeaders) {
setIsLoading(true)

props
.getAuthHeaders()
.then((authResult) => {
return fetch(props.src, {
headers: authResult.headers,
})
})
.then((response) => {
return response.blob()
})
.then((blob) => {
const newObjectUrl = URL.createObjectURL(blob)
setObjectUrl(newObjectUrl)
})
.catch(() => {
setErrorMessage('Image failed to load')
setIsLoading(false)
})
}
}, [props.src, props.getAuthHeaders])

function handleImageError(): void {
setErrorMessage('Image failed to load')
Expand All @@ -31,15 +57,17 @@ export default function Image({
<figure>
{isLoading && <CircularProgress data-cy="spinner" />}
{props.title && <Typography variant="h6">{props.title}</Typography>}
<img
{...getSizeStyles(props.width, props.height)}
src={props.src}
alt={alt}
onError={handleImageError}
onLoad={() => {
setIsLoading(false)
}}
/>
{(!props.getAuthHeaders || objectUrl) && (
<img
{...getSizeStyles(props.width, props.height)}
src={props.getAuthHeaders ? objectUrl : props.src}
alt={alt}
onError={handleImageError}
onLoad={() => {
setIsLoading(false)
}}
/>
)}
{props.description && (
<figcaption>
<MarkedMarkdown text={props.description} />
Expand Down
2 changes: 2 additions & 0 deletions src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export interface ImageFormat extends VisualizationFormat {
width?: number
/** Height rendered in pixels. */
height?: number
/** A function that can be used to get the headers for the url requests. */
getAuthHeaders?: GetAuthHeaders
}

export interface TableHeader {
Expand Down