# react-auth-image

> React components for rendering images from secured URLs with bearer authentication

Latest version **1.0.4** (published 2024-02-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-auth-image
pnpm add react-auth-image
yarn add react-auth-image
bun add react-auth-image
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2024-02-01 |
| First published | 2023-12-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 45.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Chien-Chih Yeh |
| Maintainers | chienchihyeh |
| Keywords | react, react-component, component, image, token, bearer-token, authentication |

## Links

- npm: https://www.npmjs.com/package/react-auth-image
- Repository: https://github.com/ChienChihYeh/react-auth-image
- Homepage: https://github.com/ChienChihYeh/react-auth-image#readme
- Issues: https://github.com/ChienChihYeh/react-auth-image/issues
- npm.io page: https://npm.io/package/react-auth-image

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^18.2.0
- [react-dom](https://npm.io/package/react-dom.md) ^18.2.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2024-02-01
- 1.0.5-beta (beta) — 2024-03-06
- 1.0.3 — 2024-01-02
- 1.0.2 — 2023-12-30
- 1.0.1 — 2023-12-30
- 1.0.0-9 — 2023-12-29
- 1.0.0-8 — 2023-12-29
- 1.0.0-7 — 2023-12-28
- 1.0.0-6 — 2023-12-27
- 1.0.0-5 — 2023-12-27
- 1.0.0-4 — 2023-12-27
- 1.0.0-3 — 2023-12-27
- 1.0.0-2 — 2023-12-27
- 1.0.0-1 — 2023-12-27
- 1.0.0-0 — 2023-12-27
- … 5 more at https://npm.io/package/react-auth-image/versions

## README

# react-auth-image

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

## Installation

```bash
npm install react-auth-image
```

## Usage

### Render image using `img` element

1. Import the AuthImage component:

```javascript
import { AuthImage } from "react-auth-image";
```

2. Provide the following props to the `AuthImage` component:

```javascript
<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

1. Import the `AuthBackgroundDiv` component:

```javascript
import { AuthBackgroundDiv } from "react-auth-image";
```

2. Provide the following props to the `AuthBackgroundDiv` component:

```javascript
<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`:

```javascript
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`:

```javascript
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

<!-- CONTRIBUTORS_LIST_START -->

[@ChienChihYeh](https://github.com/ChienChihYeh)
[@shinder](https://github.com/shinder)

<!-- CONTRIBUTORS_LIST_END -->

---
_Source: https://npm.io/package/react-auth-image · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
