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

mfe-okta

v1.0.1

Published

Simple Okta auth code support for Micro-Frontends

Downloads

3

Readme

mfe-okta

This module enables Micro-Frontends to retrieve an auth code from Okta in the background, and if that fails fall back to other methods.

One problem with Micro-Frontends that call secure APIs is that they require a token. If an MFE is reusable and used in multiple host apps, the host app token or auth mechanism may not also work for the MFE. The MFE will likely need to request its own auth code to pass to its own BFF, which will be exchanged for a token and stored in the BFF.

This module simplifies that pattern, and enables a host to offer that functionality to MFEs. The reason the host should do this is so that an MFE doesn't decide on its own to redirect out of a SPA and lose state, for example. The host app must be able to preserve its own state during an auth code redirect.

Example Usage

Host App

import useOktaAuthCode from 'mfe-okta';

const App = ()=> {
  let [getAuthCode, redirectState] = useOktaAuthCode({
    
    // log messages to console for debugging
    debug: true,
    
    // This method allows the host to set a "state" identifier.
    // If a full-page Okta redirect happens, this state will be
    // passed back in so the host all can restore the state and UI.
    // It is passed the config object passed in by the MFE.
    getState: (callerConfig) => {
      return "STATE-EXAMPLE";
    },
    
    // The redirectUri to pass to Okta. This is the default value
    // computed, but you should set it to whatever your app needs.
    redirectUri: window.location.origin + window.location.pathname,
    
    // The scopes to request. Since we are only getting an auth code,
    // openid is sufficient but is configurable just in case.
    scopes:['openid'],
    
    // Methods to use to try to get an auth code (tried in this order).
    // useBackground==token.getWithoutPrompt() from the SDK.
    // This requires 3rd-party cookies if your auth server is on a
    // different domain than your app, and will fail in Safari.
    // usePopupFallback==token.getWithPopup() from the SDK.
    // This causes a popup to open and then close. It avoids a full-page
    // redirect but is not very user-friendly.
    // useRedirectFallback==token.getWithRedirect() from the SDK.
    // This should always work as a fallback. Your app must compute its 
    // state and also be written to support coming back into the app and
    // restoring its state.
    useBackground: true,
    usePopupFallback: false,
    useRedirectFallback: true
  });
  
  // If a full-page redirect has happened and the user is coming back
  // into the app, redirectState will be set to the state you passed in.
  // The auth code that was passed in from Okta will be parsed from the url
  // and stored so when the MFE requests it again (since the app is being
  // rendered for the first time again) the module will pass it back.
  if (redirectState) {
    // Restore the state!
    // Pseudo-code, just an example:
    stateManager.setState(redirectState);
    navigate(routes[stateManager.getCurrentState()]);
  }

  // Pass the getAuthCode method to the MFE to call
  return <MFEComponent getAuthCode={getAuthCode}/>;
}

MFE

const MFEComponent = (props)=>{
  let [authCode,setAuthCode] = React.useState(null);
  React.useEffect(async()=>{
    // On initial load, retrieve an auth code from the host so we can call our API.
    // Must pass in your authorization server url and clientId.
    let authCode = await props.getAuthCode({
      issuer:'https://okta.mysite.com/oauth2/default',
      clientId: '000000000000'
    });
    setAuthCode(authCode);
    // Now send auth code to our BFF so it can get a token
  },[]);
}