3.9.5 • Published 2 years ago

@intility/react-msal v3.9.5

Weekly downloads
14
License
MIT
Repository
-
Last release
2 years ago

react-msal is used to auth any react app through MSAL (Work in progress)

Installation

Add .npmrc file to get the library from artifactory

@intility:registry=https://artifactory.intility.io/artifactory/api/npm/npm-local/

npm

npm i @intility/react-msal

Example usage

render() {
  return (
    <MsalAuth
      options={{
        clientId: 'client_id',
        authority: 'https://login.microsoftonline.com/<TENANT>',
        scopes: ['']
      }}
      msalDataHandler={data => console.log(data)}
      loader={() => <LoaderComponent />}
    >
      <YourApp />
    </MsalAuth>
  );
}

Default options (all of these can be overwritten)

const options = {
  clientId: '', // Your app's client id
  redirect: true, // Choose between redirect and popup login
  authority: 'https://login.microsoftonline.com/common', // Allows everyone with a MS account to log in. Specify URL and tenant if you only want to allow user from an organization
  scopes: [], // Specify permission scopes
  redirectUri: window.location.origin, // Set app's current url as redirect uri
  logging: false, // Will log stuff to console
};

Props

msalDataHandler: A function that recieves the access token and user data

loader: A react component that will be rendered if no access token exists

useMsal Hook

Documentation for variables.

Example usage

import React, { useEffect } from 'react';
import { useMsal } from '@intility/react-msal';

// specify the configs outside the component
const config = {
  auth: {
    clientId: 'your-client-id', 
    redirectUri: 'redirect-uri-after-logging-in'
  }
};

const graphScopes = ['user.read'];
const someApiScopes = ['api-guid/Read.All'];
const loginRequest = {
  scopes: [].concat(graphScopes, onboardScopes)
};
const clientConfig = { 
  loginRequest, 
  redirect: false, // optional, default to false
  handleRedirectCallback: (error, response) => {}, // optional
  manualAccessToken: true // optional, default to false. Use createAuthedFetch unless you need to decode access token manually
};

const graphRequest = { scopes: graphScopes };
const apiRequest = { scopes: someApiScopes };

const App = () => {
  const { 
    user, 
    login, 
    logout, 
    createAuthedFetch, 
    authenticating, 
    getAccessToken // only if manualAccessToken is true
  } = useMsal(config, clientConfig);

  const graphFetch = createAuthedFetch(graphRequest);
  const someApiFetch = createAuthedFetch(apiRequest);

  useEffect(() => {
    if (!user) return;

    graphFetch('graph-endpoint')
    .then(response => response.json())
    .then(data => console.log(data));

    // if manualAccessToken is true in clientConfig
    getAccessToken(graphRequest)
    .then(accessToken => console.log(accessToken));
  }, [user]);
  
  return (
    <div>
      {authenticating && 
        <SomeLoader />
      }
      {!user && !authenticating && 
        <p>I am not authenticated!</p>
        <button onClick={login}>Login</button>
      }
      {user && 
        <p>I am authenticated!</p>
        <button onClick={logout}>Logout</button>
      }
    </div>
  )
}

The function created by createAuthedFetch calls fetch to the URL specified, and with the options sent in. In addition, it adds the Authorization header with the value of Bearer {accessToken}, where the access token is fetched before each request.

As it is possible to obtain access tokens from multiple resources, msal cannot handle mutliple token requests at the same time. This means that if you need to use the function created by createAuthedFetch, make sure not to run more than one at the same time, as msal doesn't fetch the subsequent tokens while it already is fetching one. This was possible in the preview versions, so this may change to support simultaneous token fetches in a later version of this package.

Example:

graphFetch('graph-endpoint')
.then(response => response.json())
.then(handleGraphData)
.then(() => someApiFetch('api-url')) // :(
.then(response => response.json())
.then(handleCustomApiData);
3.9.5

2 years ago

3.9.6-next.0

2 years ago

3.9.5-next.0

2 years ago

3.9.1-next.0

2 years ago

3.9.2-next.0

2 years ago

3.9.3-next.0

2 years ago

3.9.4-next.0

2 years ago

3.8.4-next.0

3 years ago

3.8.2

3 years ago

3.8.0

3 years ago

3.8.2-next.0

3 years ago

3.7.1-next.0

3 years ago

3.7.0

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.4

3 years ago

1.1.22

3 years ago

1.1.20

3 years ago

1.1.19

3 years ago

1.1.18

3 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.12

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

3.6.3-next.3

3 years ago

3.6.2

3 years ago

3.6.0

3 years ago

3.5.2

3 years ago

3.5.0

3 years ago

3.4.3

3 years ago

3.4.2

3 years ago

3.4.1

3 years ago

3.4.0

3 years ago

3.3.5

3 years ago

3.3.4

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.3

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.5

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.0

3 years ago