@myxtra/authentication-green

2.18.0 • Public • Published

Xtra authentication

Easily authenticate users with Xtra and allow them to navigate to the Xtra toolkit.

Demo

Prerequisites

It's important to note that this package is only to be used (and compatible) with any of the Business Operation Units (BOU) that reside within Colruyt Group.

This package is purely a frontend component and requires a Backend For Frontend (BFF) to work. As third-party cookies are no longer a viable option, this BFF will have to be built by the BOU integrating the Xtra toolkit.

1. Request access to Azure B2C

2. Set up the Backend For Frontend (BFF)

As was mentioned in the previous paragraph, the BFF has to be built by the BOU. As such, the technologies used to build the BFF are not important. However, as the BFF's primary purpose is to authenticate users, it is important that the BFF is able to communicate with Azure B2C. In this section, we will explain what API endpoints are required for this to work.

❗ The authentication component is thus only responsible for redirecting the user to the BFF.

Authentication

Authentication flow

As depicted in the image above, the authentication flow consists of these steps:

  1. The user clicks on the login button on the website of the BOU and is redirected to the login route of the BFF.
  2. The BFF makes use of the MSAL library to instantiate a confidential client application and makes use of the client credentials flow to redirect the user to the Azure B2C login page.
  3. The user logs in and is redirected to the redirect URI of the BFF. The BFF then uses the authorization code to request an access token from Azure B2C. This access token is stored in a distributed token cache (e.g. Redis) and a first party, server only session cookie is set in the user's browser.
  4. The BFF redirects the user back to the page they were on when they clicked on the login button.

Proxy

In order for the authentication component to fetch the profile of the user and ensure the user has accepted the latest terms and conditions. The BFF has to provide a proxy endpoint that will forward the requests to the API Gateway and adds the authorization header with the access token if it is present.

Examples of some endpoints that will be called by components:

BFF URL API Gateway URL
https://bff.syst.mijnxtra.be/xtra/newsletter/v1/newsletters https://api.syst.myxtra.be/newsletter/v1/newsletters
https://bff.syst.mijnxtra.be/xtra/newsletter/v1/newsletters?partnerClientId=123456 https://api.syst.myxtra.be/newsletter/v1/newsletters?partnerClientId=123456

Installation

In order to ensure the authentication web component is rendered correctly, you can import the package in two ways.

CDN

<!doctype html>
<html>
  <head>
    <script type="module">
      import registerAuthentication from 'https://unpkg.com/@myxtra/authentication-green';

      await registerAuthentication({ apiUrl: 'http://localhost:3000', environment: 'syst' });
    </script>
  </head>
  <body>
    <header>
      <xtra-authentication flyoutPlacement="bottom-end"></xtra-authentication>
    </header>
  </body>
</html>

Package manager

First install the package using your preferred package manager.

npm install @myxtra/authentication-green

yarn add @myxtra/authentication-green

pnpm add @myxtra/authentication-green

Then import the package in your application.

import registerAuthentication from '@myxtra/authentication-green';

await registerAuthentication({ apiUrl: 'http://localhost:3000', environment: 'syst' });

Configuration

The authentication package has the following configuration properties which are passed to the registerAuthentication function:

type Config = {
  // The environment on which the authentication component is running on
  environment: 'dev' | 'test' | 'syst' | 'prod';
  // The URL of the BFF the authentication component will do fetch requests to
  apiUrl: string;
  // The URL of the BFF the authentication component will perform login, logout and redirect calls to
  // ! Only necessary when different then apiUrl, because it defaults to the provided apiUrl.
  authUrl?: string;
  // The redirect url the BFF should redirect to after the user has logged in. Defaults to window.location.href
  redirectUrl?: string;
  // The ID of the commerce/BOU for which the component will show the icon when the Terms & Condition dialog is shown.
  // Defaults to 'xtra'
  commerceId?:
    | 'bioplanet'
    | 'collectgo'
    | 'collibri'
    | 'colruyt'
    | 'colruytacademy'
    | 'colruytgroup'
    | 'dats24'
    | 'dreambaby'
    | 'dreamland'
    | 'okay'
    | 'spar'
    | 'xtra';
  // Add links to custom pages on the menu (only visible to authenticated users)
  bouMenuItems?: Array<{
    label: string; // Text to display for the link
    url: string; // URL of the custom page
    icon: string; // Icon to show in front of the text (see disclaimer below)
  }>;
  country?: 'BE' | 'FR'; // Show options from specific country. Defaults to 'BE'
};

❗ You can find a list of available BOU menu item icons here.

Position

To change the direction in which the menu of the authentication component will open, you can pass a flyoutPlacement value to the component.

<xtra-authentication flyoutPlacement="bottom-start></xtra-authentication>

Possible values are:

  • top-start
  • top-end
  • bottom-start
  • bottom-end (default)

Events & window object

The authentication component will emit events and add properties to the window object. For backwards compatibility with the orange toolkit, the window properties are the same.

Window object

Everything is available under the window.xtra object. If the user is authenticated, the component will also add the following properties to the window object:

type XTRA = {
  user: {
    firstName: () => string;
    lastName: () => string;
    dob: () => number;
    mob: () => number;
    yob: () => number;
    gender: () => 'M' | 'F';
    phone: () => string;
    streetName: () => string;
    houseNr: () => string;
    box: () => string;
    zip: () => string;
    city: () => string;
    country: () => string;
    email: () => string;
    cbh: () => string;
    state: () => 'Authenticated';
  };
};

The authenticated event is sent through window.postMessage, below you can find an example of how to listen for the event:

type XtraAuthenticationMessageEventData = {
  source: 'xtra-authentication';
  isAuthenticated?: boolean;
  address?: Address;
};

const onMessage = (message: MessageEvent<XtraAuthenticationMessageEventData>) => {
  // Ensure the event wasn't sent from another domain
  if (event.origin !== new URL(window.location.href).origin) {
    return;
  }

  // Ensure the event wasn't sent from another source
  if (!event.data.source === 'xtra-authenticaiton') {
    return;
  }

  if (!event.data.isAuthenticated) {
    // TO DO: add logic in case the user is not authenticated
    return;
  }

  // TO DO: add logic when the user is authenticated (window.xtra is set here)
};

window.addEventListener('message', onMessage);

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.14.0-alpha.00alpha
2.18.0269latest

Version History

VersionDownloads (Last 7 Days)Published
2.18.0269
2.17.0133
2.16.00
2.15.00
2.14.0-alpha.00
2.13.1-alpha.01
2.12.00
2.10.00
2.9.21
2.8.21
2.8.11
2.7.00
2.6.01
2.5.10
2.5.00
2.4.03
2.3.0238
2.2.00
2.1.00
2.1.0-alpha.01
2.0.00
1.16.0425
1.15.20
1.15.10
1.15.00
1.14.50
1.14.40
1.14.30
1.14.20
1.14.10
1.14.00
1.13.00
1.12.62
1.12.51
1.12.40
1.12.30
1.12.21
1.12.10
1.12.01
1.11.00
1.11.0-alpha.00
1.10.10
1.10.01
1.10.0-alpha.50
1.10.0-alpha.10
1.10.0-alpha.00
1.9.70
1.9.50
1.9.40
1.9.20
1.9.10
1.9.00
1.9.0-alpha.01
1.8.21
1.8.01
1.7.7-alpha.10
1.7.7-alpha.01
1.7.61
1.7.50
1.7.40
1.7.30
1.7.20
1.3.10
1.3.1-alpha.00
1.3.0-alpha.00
1.2.50
1.2.40
1.2.30
1.2.20
1.2.10
1.2.00
1.2.0-alpha.20
1.2.0-alpha.10
1.2.0-alpha.00
1.1.21
1.1.11
1.1.01
1.0.00

Package Sidebar

Install

npm i @myxtra/authentication-green

Weekly Downloads

1,088

Version

2.18.0

License

none

Unpacked Size

727 kB

Total Files

8

Last publish

Collaborators

  • dcsplatformservices