Fix authenticated media loading for Tauri/WebKit environments
- Add useAuthenticatedMediaUrl hook that fetches media with auth headers - Add AuthenticatedMedia, AuthenticatedAvatarImage components - Update downloadMedia and downloadEncryptedMedia to accept access token - Update all media components to pass access token for auth media - Fix 401 errors on Matrix servers requiring authenticated media (v1.11+) This fixes media loading in Tauri desktop apps where service workers may not work reliably for cross-origin authenticated requests.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import React, { ComponentProps, forwardRef } from 'react';
|
||||
import { AvatarImage } from 'folds';
|
||||
import { useAuthenticatedMediaUrl } from '../../hooks/useAuthenticatedMediaUrl';
|
||||
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
||||
|
||||
export type AuthenticatedAvatarImageProps = ComponentProps<typeof AvatarImage>;
|
||||
|
||||
/**
|
||||
* AvatarImage component that handles Matrix authenticated media.
|
||||
* Automatically fetches authenticated media URLs with the proper Authorization header
|
||||
* and renders them as blob URLs.
|
||||
*/
|
||||
export const AuthenticatedAvatarImage = forwardRef<HTMLImageElement, AuthenticatedAvatarImageProps>(
|
||||
({ src, ...props }, ref) => {
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const authenticatedSrc = useAuthenticatedMediaUrl(src, useAuthentication);
|
||||
|
||||
return <AvatarImage ref={ref} {...props} src={authenticatedSrc} />;
|
||||
}
|
||||
);
|
||||
|
||||
AuthenticatedAvatarImage.displayName = 'AuthenticatedAvatarImage';
|
||||
@@ -0,0 +1,45 @@
|
||||
import React, { ImgHTMLAttributes, forwardRef, useEffect, useState } from 'react';
|
||||
import { useAuthenticatedMediaUrl } from '../../hooks/useAuthenticatedMediaUrl';
|
||||
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
||||
|
||||
export interface AuthenticatedMediaProps
|
||||
extends Omit<ImgHTMLAttributes<HTMLImageElement>, 'src'> {
|
||||
/** The source URL of the media (may be an authenticated media URL) */
|
||||
src?: string;
|
||||
/** Override for authentication - if not provided, uses useMediaAuthentication hook */
|
||||
useAuthentication?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Image component that handles Matrix authenticated media.
|
||||
* Automatically fetches authenticated media URLs with the proper Authorization header
|
||||
* and renders them as blob URLs.
|
||||
*/
|
||||
export const AuthenticatedMedia = forwardRef<HTMLImageElement, AuthenticatedMediaProps>(
|
||||
({ src, useAuthentication: useAuthOverride, ...props }, ref) => {
|
||||
const useAuthFromServer = useMediaAuthentication();
|
||||
const useAuthentication = useAuthOverride ?? useAuthFromServer;
|
||||
const authenticatedSrc = useAuthenticatedMediaUrl(src, useAuthentication);
|
||||
|
||||
return <img ref={ref} {...props} src={authenticatedSrc} />;
|
||||
}
|
||||
);
|
||||
|
||||
AuthenticatedMedia.displayName = 'AuthenticatedMedia';
|
||||
|
||||
/**
|
||||
* A simpler authenticated img component for use in places like custom HTML parsers.
|
||||
* This component handles its own authentication state.
|
||||
*/
|
||||
export interface AuthenticatedImgProps extends ImgHTMLAttributes<HTMLImageElement> {}
|
||||
|
||||
export const AuthenticatedImg = forwardRef<HTMLImageElement, AuthenticatedImgProps>(
|
||||
({ src, ...props }, ref) => {
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const authenticatedSrc = useAuthenticatedMediaUrl(src, useAuthentication);
|
||||
|
||||
return <img ref={ref} {...props} src={authenticatedSrc} />;
|
||||
}
|
||||
);
|
||||
|
||||
AuthenticatedImg.displayName = 'AuthenticatedImg';
|
||||
@@ -0,0 +1,4 @@
|
||||
export { AuthenticatedMedia, AuthenticatedImg } from './AuthenticatedMedia';
|
||||
export type { AuthenticatedMediaProps, AuthenticatedImgProps } from './AuthenticatedMedia';
|
||||
export { AuthenticatedAvatarImage } from './AuthenticatedAvatarImage';
|
||||
export type { AuthenticatedAvatarImageProps } from './AuthenticatedAvatarImage';
|
||||
Reference in New Issue
Block a user