npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

auth-profile-draft

v1.1.7

Published

HFN OneAuth web component

Downloads

2,439

Readme

HFN OneAuth web component

The oneAuth web component is designed to manage authentication for your application. This component can be used to trigger authentication either automatically when the page loads or on-demand based on user interactions.

Usage

To use the <hfn-oneauth> component, you need to include it in your application's layout or a specific page where authentication is required.

<hfn-oneauth config='{"authUrl":"https://authadmin.dev.heartfulness.org","realm":"heartfulness-qa","client_id":"heartfulness_website"}' showCancel="true" authType="on-demand"/></hfn-oneauth>

Attributes

config (required): The configuration for the authentication process, including the authUrl, realm, and client_id.

showCancel (optional): This attribute enables a cancel button in the error block. It requires the "LandingPage" path to be stored in localStorage. When the cancel button is clicked, the user is redirected back to the landing page.

authType (optional):

  • Controls when the authentication process is triggered.
  • "on-demand": The authentication is triggered only when explicitly called using the method document.querySelector("hfn-oneauth").triggerAuth(). This is useful for scenarios where authentication should only occur after a specific user action, like clicking a button.
  • If authType is not set, the authentication process starts automatically as soon as the page loads.

Handling Authentication

By default, the redirect URI is set to /oneauth/authorization. After successful authentication through HFN OneAuth, the user is redirected to the /oneauth/authorization route. This route must be created in your application to handle the authorization response, process the received token, and then redirect the user back to the landing page stored in localStorage.

Simple HTML Implementation

<!---
<html>
    <head>  
        <script type="module" src="./dist/main.js"></script>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
        <script type="text/javascript">
            window.addEventListener('load', (res) => {
                const authComp = document.querySelector('hfn-oneauth');

                // After successful login, We will get Auth token from the below callback method. Using this token we can fetch SRCM profile information.
                authComp.loginCallback = function(res){
                    if(res?.data?.access_token){
                        fetch(`https://xxx/me`, {
                            method:"GET",
                            headers: {
                                Accept: "application/json",
                                Authorization: `Bearer ${res?.data?.access_token}`,
                                "x-client-id":"xxx",
                            }
                        })
                        .then((r) => {
                            if (r?.data?.results.length > 0) {
                            }
                        })
                        .catch((e) => {
                            //In the event of an error, an error block can be displayed, offering options to retry login or cancel, utilizing the handleProfileAuthentication function.
                           authComp.handleProfileAuthentication(false);
                        });
                    }
                };
            });
        </script>
    </head>
    <body>
        <div>
            <hfn-oneauth config='{"authUrl":"https://xxx.com","realm":"xxx","client_id":"xxx"}'  showCancel="true"/></hfn-oneauth>
        </div>
    </body>
</html>
-->

React & Gatsby Implementation

npm i hfn-oneauth

In React or Gatsby App:

Handle authentication in "/oneauth/authorization" page

<!-- 
const hfnAuth = useRef(); 

React.useEffect(() => {
    import("hfn-oneauth/main");
});

useEffect(() => {

    // After successful login, We will get Auth token from the below callback method. Using this token we can fetch SRCM profile information. 

    hfnAuth.current.loginCallback = (res) => {
        fetch(`{baseURL}/xxx/me/`, {
            headers: {
                Accept: "application/json",
                Authorization: `Bearer res?.access_token`,
            }
        })
        .then((r) => {
            if (r?.data?.results.length > 0) {
                console.log(r?.data?.results)
            }
        })
        .catch((e) => {
            //In the event of an error, an error block can be displayed, offering options to retry login or cancel, utilizing the handleProfileAuthentication function.
           hfnAuth.current.handleProfileAuthentication(false);
        });
    };
}, [hfnAuth]); 

return (
    <hfn-oneauth config='{"authUrl":"https://xxx.com","realm":"xxx","client_id":"xxx"}'  showCancel="true" authType="on-demand" /></hfn-oneauth>
);
-->

Get new active token using refresh token & Logout utils.

<!-- 
import { getRefreshToken, userLogout } from "hfn-oneauth/main";


// Retrive new token from refresh token
const getActiveToken = async () => {
    const params = {
        "authUrl":"https://xxx.com",
        "realm":"xxx",
        "client_id":"xxx"
    };
    const tokenData = await getRefreshToken(params).catch(() => {
        // console.log(e);
      });
    console.log(tokenData);
};

// Logout - Clear keycloak session
const logout = async () => {
    const params = {
        "authUrl":"https://xxx.com",
        "realm":"xxx",
        "client_id":"xxx"
    };
    const res = userLogout(params);
    if (!res?.error) {
        // logout success
    }
};

<Button onClick={getActiveToken} className="logout-btn btn-cls">
    Active Token
</Button>
<Button onClick={logout} className="logout-btn btn-cls">
    Logout
</Button>

-->

NextJS Implementation

npm i hfn-oneauth

In NextJS App:

Handle authentication in "/oneauth/authorization" page

<!--
const hfnAuth = useRef();

React.useEffect(() => {
    import("hfn-oneauth/main");
});

useEffect(() => {

    // After successful login, We will get Auth token from the below callback method. Using this token we can fetch SRCM profile information.

    hfnAuth.current.loginCallback = (res) => {
        fetch(`{baseURL}/xxx/me/`, {
            headers: {
                Accept: "application/json",
                Authorization: `Bearer res?.access_token`,
            }
        })
        .then((r) => {
            if (r?.data?.results.length > 0) {
                console.log(r?.data?.results)
            }
        })
        .catch((e) => {
            //In the event of an error, an error block can be displayed, offering options to retry login or cancel, utilizing the handleProfileAuthentication function.
           hfnAuth.current.handleProfileAuthentication(false);
        });
    };
}, [hfnAuth]);

return (
    <hfn-oneauth config='{"authUrl":"https://xxx.com","realm":"xxx","client_id":"xxx"}'  showCancel="true" authType="on-demand" /></hfn-oneauth>
);

Get new active token using refresh token & Logout utils.

<!-- 

// Retrive new token from refresh token
const getActiveToken = async () => {
    if (typeof window !== "undefined") {
        const { getRefreshToken } = await import("hfn-oneauth/main");
    const params = {
        "authUrl":"https://xxx.com",
        "realm":"xxx",
        "client_id":"xxx"
    };
        const tokenData = await getRefreshToken(params).catch(() => {
            // console.log(e);
        });
        console.log(tokenData);
    }
};

// Logout - Clear keycloak session
const logout = async () => {
    const params = {
        "authUrl":"https://xxx.com",
        "realm":"xxx",
        "client_id":"xxx"
    };
    if (typeof window !== "undefined") {
        const { userLogout } = await import("hfn-oneauth/main");
        const res = await userLogout(params);
        if (!res?.error) {
            // Successfully logout
        }
    }
};

<Button onClick={getActiveToken} className="logout-btn btn-cls">
    Active Token
</Button>
<Button onClick={logout} className="logout-btn btn-cls">
    Logout
</Button>

-->