You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

react-auth-image

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-auth-image

React components for rendering images from secured URLs with bearer authentication

1.0.4
latest
Source
npmnpm
Version published
Weekly downloads
31
82.35%
Maintainers
1
Weekly downloads
 
Created
Source

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

  • Import the AuthImage component:
import { AuthImage } from "react-auth-image";
  • 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

  • Import the AuthBackgroundDiv component:
import { AuthBackgroundDiv } from "react-auth-image";
  • 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

Keywords

react

FAQs

Package last updated on 01 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts