Skip to content

Commit 62e3de6

Browse files
fix(react): add auth tokens to image attachment URLs
1 parent 2401520 commit 62e3de6

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

packages/react/src/views/AttachmentHandler/ImageAttachment.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useContext } from 'react';
1+
import React, { useState, useContext, useEffect } from 'react';
22
import { css } from '@emotion/react';
33
import PropTypes from 'prop-types';
44
import { Box, Avatar, useTheme } from '@embeddedchat/ui-elements';
@@ -16,6 +16,23 @@ const ImageAttachment = ({
1616
}) => {
1717
const { RCInstance } = useContext(RCContext);
1818
const [showGallery, setShowGallery] = useState(false);
19+
const [authParams, setAuthParams] = useState('');
20+
21+
useEffect(() => {
22+
let cancelled = false;
23+
RCInstance.auth.getCurrentUser().then((user) => {
24+
if (!cancelled && user?.authToken && user?.userId) {
25+
setAuthParams(`rc_token=${user.authToken}&rc_uid=${user.userId}`);
26+
}
27+
});
28+
return () => { cancelled = true; };
29+
}, [RCInstance]);
30+
31+
const withAuth = (url) => {
32+
if (!url || !authParams) return url;
33+
const sep = url.includes('?') ? '&' : '?';
34+
return `${url}${sep}${authParams}`;
35+
};
1936
const getUserAvatarUrl = (icon) => {
2037
const instanceHost = RCInstance.getHost();
2138
const URL = `${instanceHost}${icon}`;
@@ -96,7 +113,7 @@ const ImageAttachment = ({
96113
{isExpanded && (
97114
<Box onClick={() => setShowGallery(true)}>
98115
<img
99-
src={host + attachment.image_url}
116+
src={withAuth(host + attachment.image_url)}
100117
style={{
101118
maxWidth: '100%',
102119
objectFit: 'contain',
@@ -160,7 +177,7 @@ const ImageAttachment = ({
160177
variantStyles={variantStyles}
161178
/>
162179
<img
163-
src={host + nestedAttachment.image_url}
180+
src={withAuth(host + nestedAttachment.image_url)}
164181
style={{
165182
maxWidth: '100%',
166183
objectFit: 'contain',

0 commit comments

Comments
 (0)