Skip to content

JagarYousef/react-protected-src

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-protected-src

Use custom headers in <img> src attribute easily.
With this hook you can use custom headers (e.g. JWT token) directly in src without worrying about the extra steps, and then you get your clean code.


1. Installation:

npm i react-protected-src

or with yarn:

yarn add react-protected-src

2. Import:

import {useProtectedSrc} from "react-protected-src";

3. Usage:

export function Page() {
    const base64 = useProtectedSrc('http://example.com/image.jpg', {'Authorization': 'Bearer SOME_TOKEN'})
    return (
        <div>
            <img src={base64} />
        </div>
    )
}

nextJS example:

export default function Home() {
    const base64 = useProtectedSrc('http://example.com/image.jpg', {'Authorization': 'Bearer SOME_TOKEN'})
    return (
        <div>
            <Image src={base64} width={100} height={100} />
        </div>
    )
}

About

Use headers values when loading images in `src` attributes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published