1.0.4 • Published 2 years ago
react-auth-image v1.0.4
react-auth-image
React components for rendering images from protected URLs using authorization bearer token.
Installation
npm install react-auth-imageUsage
Render image using img element
- Import the AuthImage component:
import { AuthImage } from "react-auth-image";- Provide the following props to the
AuthImagecomponent:
<AuthImage
src={url}
token={token}
errorCallback={handleError}
{...HTMLImageElementAttributes}
/>AuthImage Props
| Prop | Type | Description |
|---|---|---|
src | string | (Required) The source URL for the image. |
token | string | (Required) The authentication token for accessing the image. This token will be added to the Authorization header as Bearer ${token}. |
errorCallback | function | An optional callback function to handle errors when accessing the image. |
| Other Attributes | - | Any additional HTML image element attributes can be passed as props. |
Render image using div element background
- Import the
AuthBackgroundDivcomponent:
import { AuthBackgroundDiv } from "react-auth-image";- Provide the following props to the
AuthBackgroundDivcomponent:
<AuthBackgroundDiv
url={url}
token={token}
errorCallback={handleError}
{...HTMLImageElementAttributes}
>
{/* Children can be added here */}
<p>This is a child</p>
</AuthBackgroundDiv>AuthBackgroundDiv Props
| Prop | Type | Description |
|---|---|---|
url | string | (Required) The source URL for the image. |
token | string | (Required) The authentication token for accessing the image. This token will be added to the Authorization header as Bearer ${token}. |
errorCallback | function | An optional callback function to handle errors when accessing the image. |
| Other Attributes | - | Any additional HTML div element attributes can be passed as props. |
Example
Render image using AuthImage:
import { AuthImage } from "react-auth-image";
const MyComponent = () => {
const url = "https://example.com/image.jpg";
const token = "your-auth-token";
const handleError = (error) => {
console.error(`Error rendering image: ${error.message}`);
};
return (
<AuthImage
src={url}
token={token}
errorCallback={handleError}
alt="example-image"
className="my-image"
/>
);
};Render image using AuthBackgroundDiv:
import { AuthBackgroundDiv } from "react-auth-image";
const MyComponent = () => {
const url = "https://example.com/image.jpg";
const token = "your-auth-token";
const handleError = (error) => {
console.error(`Error rendering image: ${error.message}`);
};
return (
<AuthBackgroundDiv
url={url}
token={token}
errorCallback={handleError}
className="my-div"
>
<p>This is a child</p>
</AuthBackgroundDiv>
);
};Contributors
1.0.5-beta
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0-7
2 years ago
1.0.0-9
2 years ago
1.0.0-8
2 years ago
1.0.0-6
2 years ago
1.0.0-5
2 years ago
1.0.0-4
2 years ago
1.0.0-3
2 years ago
1.0.0-2
2 years ago
1.0.0-1
2 years ago
1.0.0-0
2 years ago
0.3.13
2 years ago
0.3.12
2 years ago
0.3.11
2 years ago
0.3.10
2 years ago
0.3.9
2 years ago