|
1 |
| -# Project Name |
2 |
| - |
3 |
| -(short, 1-3 sentenced, description of the project) |
4 |
| - |
5 |
| -## Features |
6 |
| - |
7 |
| -This project framework provides the following features: |
8 |
| - |
9 |
| -* Feature 1 |
10 |
| -* Feature 2 |
11 |
| -* ... |
12 |
| - |
13 |
| -## Getting Started |
14 |
| - |
15 |
| -### Prerequisites |
16 |
| - |
17 |
| -(ideally very short, if any) |
18 |
| - |
19 |
| -- OS |
20 |
| -- Library version |
21 |
| -- ... |
22 |
| - |
23 |
| -### Installation |
24 |
| - |
25 |
| -(ideally very short) |
26 |
| - |
27 |
| -- npm install [package name] |
28 |
| -- mvn install |
29 |
| -- ... |
30 |
| - |
31 |
| -### Quickstart |
32 |
| -(Add steps to get up and running quickly) |
33 |
| - |
34 |
| -1. git clone [repository clone url] |
35 |
| -2. cd [respository name] |
36 |
| -3. ... |
37 |
| - |
38 |
| - |
39 |
| -## Demo |
40 |
| - |
41 |
| -A demo app is included to show how to use the project. |
42 |
| - |
43 |
| -To run the demo, follow these steps: |
44 |
| - |
45 |
| -(Add steps to start up the demo) |
46 |
| - |
47 |
| -1. |
48 |
| -2. |
49 |
| -3. |
50 |
| - |
51 |
| -## Resources |
52 |
| - |
53 |
| -(Any additional resources or related projects) |
54 |
| - |
55 |
| -- Link to supporting information |
56 |
| -- Link to similar sample |
57 |
| -- ... |
| 1 | +## React AAD MSAL |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +### Overview |
| 6 | +React AAD MSAL is a library to easily integrate the Microsoft Authentication Library with Azure Active Directory in your React app quickly and reliably. The library focuses on flexibility, allowing you to define how you want to interact with logins and logouts. |
| 7 | +### Setup |
| 8 | +In the render module of your component, make sure to create an AzureAD component with the arguments you need. This uses the functions that you will define. Once the user is successfully authenticated, the component will render the OnAuthenticationComponent given. This is where you should put the secure, user-specific parts of your app. `loginCallback` and `printUserInfo` can be any user defined functions. |
| 9 | + |
| 10 | + |
| 11 | +Find the assignment for ClientID and replace the value with the Application ID for your application from the azure portal. The authority is the sign-in/signup policy for your application. Graph scopes is a list of scope URLs that you want to grant access to. You can find more information on the [active directory MSAL single page app azure sample](https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp). |
| 12 | +```javascript |
| 13 | +// ... |
| 14 | + |
| 15 | + return ( |
| 16 | + <AzureAD |
| 17 | + clientID={'<Application ID for your application>'} |
| 18 | + graphScopes={['https://<your-tenant-name>.onmicrosoft.com/hello/demo.read']} |
| 19 | + unauthenticatedFunction={this.loginCallback} |
| 20 | + authenticatedFunction={this.logoutCallback} |
| 21 | + userInfoCallback={this.printUserInfo} |
| 22 | + authority={'https://login.microsoftonline.com/tfp/<your-tenant-name>.onmicrosoft.com/<your-sign-in-sign-up-policy>'} |
| 23 | + type={LoginType.Popup}> |
| 24 | + <OnAuthenticationComponent /> |
| 25 | + </AzureAD> |
| 26 | +); |
| 27 | +``` |
| 28 | +### Login |
| 29 | +To login, first create a callback function for the AzureAD component to consume. This function will be called when the component loads, and it will pass in the function to be called when the user wants to login. In this case, we create a button that will log the user in. |
| 30 | +```javascript |
| 31 | +import AzureAD from 'AzureAD' |
| 32 | + |
| 33 | +loginCallback = (login) => { |
| 34 | + return <button onclick={login}>Login</button>; |
| 35 | +}; |
| 36 | +// ... |
| 37 | +``` |
| 38 | +Once they're logged in, the AzureAD library will call another function given with an `IUserInfo` instance. You can do whatever you want with this, but you should store it. In this example, we just print it out to console. |
| 39 | +```javascript |
| 40 | +//... |
| 41 | +printUserInfo = (userInfo) => {console.log(userInfo)}; |
| 42 | +//... |
| 43 | +``` |
| 44 | + |
| 45 | +Once you've set this up, you should be able to set up a button to login that will hit an AAD instance. To set up your instance, check out the documentation on [Azure Active Directory](https://docs.microsoft.com/en-us/azure/active-directory-b2c/) and on how to connect an [Identity Provider](https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-setup-msa-app) for that AAD instance. |
| 46 | + |
| 47 | +### Logout |
| 48 | + |
| 49 | +Logging out is just as easy. |
| 50 | +```javascript |
| 51 | +logoutCallback = (logout) => { |
| 52 | + return <button onclick={logout}>Logout</button>; |
| 53 | +}; |
| 54 | +``` |
| 55 | +You can, of course, include a component in either of these functions. This allows you to gate which view of your application users get, based on whether or not they are authenticated. |
| 56 | + |
| 57 | +### Samples |
| 58 | + |
| 59 | +If you want to run examples of this library out of the box, feel free to go to [the samples repo](https://reactaad.visualstudio.com/react-aad-msal/). There you'll find a couple implementations that leverage the library, as well as a tutorial of how to set up Azure Active Directory with an Identity Provider. |
| 60 | + |
| 61 | +### Integrating with a Redux Store |
| 62 | + |
| 63 | +The Azure AD component optionally accepts a ```reduxStore``` prop. On successful login, Azure AD will dispatch an action of type ```AAD_LOGIN_SUCCESS``` to the provided store, containing the token and user information returned from Active Directory. It does the same for logout events, but the action will not contain a payload. |
| 64 | + |
| 65 | +Import your store into the file rendering the AzureAD component and pass it in: |
| 66 | + |
| 67 | +```javascript |
| 68 | +<AzureAD |
| 69 | + reduxStore={store} |
| 70 | + clientID={'<Application ID for your application>'} |
| 71 | + graphScopes={['https://<your-tenant-name>.onmicrosoft.com/hello/demo.read']} |
| 72 | + unauthenticatedFunction={this.loginCallback} |
| 73 | + authenticatedFunction={this.logoutCallback} |
| 74 | + userInfoCallback={this.printUserInfo} |
| 75 | + authority={'https://login.microsoftonline.com/tfp/<your-tenant-name>.onmicrosoft.com/<your-sign-in-sign-up-policy>'} |
| 76 | + type={LoginType.Popup} |
| 77 | +/> |
| 78 | +``` |
| 79 | + |
| 80 | +Add a case to handle ```AAD_LOGIN_SUCCESS``` and ```AAD_LOGOUT_SUCCESS``` actions in a reducer file: |
| 81 | + |
| 82 | +```javascript |
| 83 | +const initialState = { |
| 84 | + aadResponse: null, |
| 85 | +}; |
| 86 | + |
| 87 | +const sampleReducer = (state = initialState, action) => { |
| 88 | + switch (action.type) { |
| 89 | + case 'AAD_LOGIN_SUCCESS': |
| 90 | + return { ...state, aadResponse: action.payload }; |
| 91 | + case 'AAD_LOGOUT_SUCCESS': |
| 92 | + return { ...state, aadResponse: null}; |
| 93 | + default: |
| 94 | + return state; |
| 95 | + } |
| 96 | +}; |
| 97 | +``` |
0 commit comments