2.0.1 • Published 1 year ago

react-microsoft-login v2.0.1

Weekly downloads
2,528
License
ISC
Repository
github
Last release
1 year ago

react-microsoft-login

npm npm bundle size npm

React component for a simple login with Microsoft services, based on Official Microsoft Authentication Library for JavaScript.

DEMO HERE

🚀 Get started

  1. Install package:
    npm i react-microsoft-login
    # or
    yarn add react-microsoft-login
  2. Import and configure component:

    import React from "react";
    import MicrosoftLogin from "react-microsoft-login";
    
    export default (props) => {
      const authHandler = (err, data) => {
        console.log(err, data);
      };
    
      return (
        <MicrosoftLogin clientId={YOUR_CLIENT_ID} authCallback={authHandler} />
      );
    };
  3. YOUR_CLIENT_ID is the key which you need to generate for your Microsoft app. How to create Microsoft app? When finished, on the app Overview page, note down the Application (client) ID value.

📖 API

ParameterTypeDefaultDescription
clientIdstringrequiredApplication (client) ID
authCallbackfunctionrequiredCallback function which takes three arguments (error, authData, msalInstance)
graphScopesarray["user.read"]Array of Graph API permission names. More about Graph API permissions.
redirectUristringwindow.location.hrefThe redirect URI of the application, this should be same as the value in the application registration portal.
postLogoutRedirectUristringYou can configure the URI to which it should redirect after sign-out by setting postLogoutRedirectUri. This URI should also be registered as the logout URI in your application registration.
tenantUrlstringA URL indicating a directory that MSAL can request tokens from. More about MSAL tenant auth.
promptenum("login", "select_account", "consent", "none")Specify custom prompt behavior
buttonThemeenum("dark_short", "light_short", "dark", "light")"light"Theme for button style that based on Official Microsoft brand design.
withUserDatabooleanBoolean flag to make an additional request to GraphAPI to get user data.
forceRedirectStrategybooleanBoolean flag to force redirect login strategy for all browsers. This strategy used by default just for IE browsers to avoid issues.
useLocalStorageCachebooleanYou can set cookie storage to localStorage for persistent login between tabs and sessions. Session storage is used by default. More about SSO with MSAL.
debugbooleanBoolean flag to enable detailed logs of authorization process.
classNamestringAdditional class name string.
childrenReactComponentAlternative way to provide custom button element as a children prop instead of Official Microsoft brand design

Sign out

Since version 1.12.0 and higher msalInstance returned as third argument in callback function, after success auth. With this instance you can do many things and logout is one of them:

import React, { useState } from "react";
import MicrosoftLogin from "../../dist";

const ExamplePage = () => {
  const [msalInstance, onMsalInstanceChange] = useState();

  const loginHandler = (err, data, msal) => {
    console.log(err, data);
    // some actions
    if (!err && data) {
      onMsalInstanceChange(msal);
    }
  };

  const logoutHandler = () => {
    msalInstance.logout();
  };

  return msalInstance ? (
    <button onClick={logoutHandler}>Logout</button>
  ) : (
    <MicrosoftLogin clientId={clientId} authCallback={loginHandler} />
  );
};

export default ExamplePage;

📝 License

MIT

2.0.1

1 year ago

2.0.0

1 year ago

1.15.0

3 years ago

1.14.0

3 years ago

1.13.0

3 years ago

1.13.6

3 years ago

1.13.4

3 years ago

1.13.3

3 years ago

1.11.3

3 years ago

1.12.1

3 years ago

1.12.0

3 years ago

1.11.0

3 years ago

1.10.3

3 years ago

1.10.2

3 years ago

1.10.1

4 years ago

1.10.0

4 years ago

1.9.0

4 years ago

1.8.5

4 years ago

1.8.4

4 years ago

1.8.2

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago