1.0.4 • Published 2 months ago

react-auth-image v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

react-auth-image

React components for rendering images from protected URLs using authorization bearer token.

Installation

npm install react-auth-image

Usage

Render image using img element

  1. Import the AuthImage component:
import { AuthImage } from "react-auth-image";
  1. Provide the following props to the AuthImage component:
<AuthImage
  src={url}
  token={token}
  errorCallback={handleError}
  {...HTMLImageElementAttributes}
/>

AuthImage Props

PropTypeDescription
srcstring(Required) The source URL for the image.
tokenstring(Required) The authentication token for accessing the image. This token will be added to the Authorization header as Bearer ${token}.
errorCallbackfunctionAn 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

  1. Import the AuthBackgroundDiv component:
import { AuthBackgroundDiv } from "react-auth-image";
  1. Provide the following props to the AuthBackgroundDiv component:
<AuthBackgroundDiv
  url={url}
  token={token}
  errorCallback={handleError}
  {...HTMLImageElementAttributes}
>
  {/* Children can be added here */}
  <p>This is a child</p>
</AuthBackgroundDiv>

AuthBackgroundDiv Props

PropTypeDescription
urlstring(Required) The source URL for the image.
tokenstring(Required) The authentication token for accessing the image. This token will be added to the Authorization header as Bearer ${token}.
errorCallbackfunctionAn 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

@ChienChihYeh @shinder

1.0.5-beta

2 months ago

1.0.4

3 months ago

1.0.3

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0-7

4 months ago

1.0.0-9

4 months ago

1.0.0-8

4 months ago

1.0.0-6

4 months ago

1.0.0-5

4 months ago

1.0.0-4

4 months ago

1.0.0-3

4 months ago

1.0.0-2

4 months ago

1.0.0-1

5 months ago

1.0.0-0

5 months ago

0.3.13

5 months ago

0.3.12

5 months ago

0.3.11

5 months ago

0.3.10

5 months ago

0.3.9

5 months ago