|
| 1 | +# react-aad-msal Sample App |
| 2 | + |
| 3 | +This repository contains a sample react application that demonstrates how to use the [`react-aad-msal`](https://www.npmjs.com/package/react-aad-msal) node module, an Azure Activity Directory react component. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +React AAD MSAL is a library that allows you to easily integrate auth using Azure Active Directory into your React application. The library focuses on flexibility, allowing you to define how you want to interact with logins and logouts. |
| 8 | + |
| 9 | +The React AAD MSAL library provides the following features: |
| 10 | + |
| 11 | +* Login using Azure Active Directory |
| 12 | + - create your own function that handles how login (using this AzureAD component) is trigger in your react app |
| 13 | + - create your own function that handles the login success. The AzureAD library will call this function when login is complete to pass back the user info. |
| 14 | +* Logout callback |
| 15 | + - create your own function to handle how logout (using this AzureAD component) is trigger in your react app |
| 16 | +* Optional use of redux store containing the token and user information returned from Active Directory |
| 17 | + |
| 18 | +### Prerequisites |
| 19 | + |
| 20 | +*Setting up an Azure Active Directory App* |
| 21 | + |
| 22 | +In order to use this sample, you must have an Azure Active Directory application setup. |
| 23 | + |
| 24 | +Documentation for AAD Application: |
| 25 | + |
| 26 | +https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal |
| 27 | + |
| 28 | +Documentation for AAD B2C Application: |
| 29 | + |
| 30 | +https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-app-registration |
| 31 | + |
| 32 | +## Getting Started |
| 33 | + |
| 34 | +*NOTE: in order to successfully build and run this sample, be sure to complete the prerequisite steps above. |
| 35 | +An Azure Active Directory application must first be setup and configured.* |
| 36 | + |
| 37 | +- Build the `react-aad-msal` component: `npm install && npm run build` |
| 38 | +- create a `.env.local` file, with the following variables: |
| 39 | + ``` |
| 40 | + REACT_APP_AAD_APP_CLIENT_ID=<client id guid> |
| 41 | + REACT_APP_AUTHORITY=<authority url (optional)> |
| 42 | + ``` |
| 43 | +- Run the sample application: `npm install && npm run start` |
| 44 | + |
| 45 | +The sample site should launch in a Web browser. |
| 46 | + |
| 47 | +### Quickstart |
| 48 | + |
| 49 | +1. `git clone [repository clone url]` |
| 50 | +2. `cd react-aad-msal` |
| 51 | +3. `npm install` |
| 52 | +4. Setup a `.env` file with the following items: |
| 53 | + - `REACT_APP_AAD_APP_CLIENT_ID` |
| 54 | + - `REACT_APP_AUTHORITY` |
| 55 | +5. `npm start` |
| 56 | + |
| 57 | +## Demo |
| 58 | + |
| 59 | +This sample demonstrates how to use the `Popup` auth method. As well as how to use the (optional) redux store. |
| 60 | + |
| 61 | +To run this sample, you just need to provide your `REACT_APP_AAD_APP_CLIENT_ID` and (optionally) `REACT_APP_AUTHORITY`. |
| 62 | + |
| 63 | +``` javascript |
| 64 | +<AzureAD |
| 65 | + clientID={process.env.REACT_APP_AAD_APP_CLIENT_ID} |
| 66 | + authority={process.env.REACT_APP_AUTHORITY} |
| 67 | + ... |
| 68 | + > |
| 69 | +``` |
| 70 | + |
| 71 | +Type is set to `LoginType.Popup`. |
| 72 | + |
| 73 | +``` javascript |
| 74 | +<AzureAD |
| 75 | + ... |
| 76 | + type={LoginType.Popup} |
| 77 | + ... |
| 78 | + > |
| 79 | +``` |
| 80 | + |
| 81 | +And we also provide a reduxStore (setup in our `reduxStore.js` file). |
| 82 | + |
| 83 | +``` javascript |
| 84 | +import { basicReduxStore } from './reduxStore'; |
| 85 | + |
| 86 | +<AzureAD |
| 87 | + ... |
| 88 | + reduxStore={basicReduxStore} |
| 89 | + ... |
| 90 | +> |
| 91 | +``` |
| 92 | + |
| 93 | +For our `userInfoCallback` property, we setup a function that just saves the userInfo we get back from AAD to state. |
| 94 | + |
| 95 | +``` javascript |
| 96 | +userJustLoggedIn = receivedUserInfo => { |
| 97 | + this.setState({ userInfo: receivedUserInfo }) |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +For our `unauthenticatedFunction` property, we setup a function that returns a button that uses the login function provided by our AzureAD component. |
| 102 | + |
| 103 | +``` javascript |
| 104 | +unauthenticatedFunction = loginFunction => { |
| 105 | + return ( |
| 106 | + <button style={buttonStyle} onClick={loginFunction}>Login</button> |
| 107 | + ); |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +//TODO: Logout documentation |
| 112 | + |
| 113 | +## Resources |
| 114 | + |
| 115 | +- Link to react-aad-msal library github |
| 116 | +- Link to react-aad-msal npm page |
0 commit comments