You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
7
8
+
## Setup
9
+
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 JSX returned by the `authenticatedFunction`, which in this case is called `logoutCallback`. This is where you should put the secure, user-specific parts of your app. `loginCallback` and `printUserInfo` can be any user defined functions.
10
10
11
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
+
12
13
```javascript
13
-
// ...
14
+
// ...
14
15
15
16
return (
16
17
<AzureAD
17
18
clientID={'<Application ID for your application>'}
-`clientID`: String representing your Azure Active Directory Application ID
34
+
35
+
-`scopes`: Array of permission scopes you want to request from the application you are authenticating against. You can see possible [values for this property here](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-scopes)
36
+
37
+
-`authenticatedFunction`: A user defined callback function for the AzureAD component to consume. This function receives the AzureAD components logout function, and returns JSX containing the logged in portion of your app. You can use this received logout callback to attach it to any part of your logged in portion of your application.
38
+
39
+
-`unauthenticatedFunction`: A user defined callback function for the AzureAD component to consume. This function receives the AzureAD components login function which you can then use to trigger a login as you like
40
+
41
+
-`userInfoCallback`: A user defined callback function. The AzureAD library will calls this function when login is complete to pass back the user info in the following format:
42
+
43
+
```javascript
44
+
IUserInfo {
45
+
jwtAccessToken: string,
46
+
jwtIdToken: string,
47
+
user:Msal.User
48
+
}
49
+
```
50
+
51
+
The format of [`Msal.User` can be found here](https://htmlpreview.github.io/?https://raw.githubusercontent.com/AzureAD/microsoft-authentication-library-for-js/dev/docs/classes/_user_.user.html)
52
+
53
+
- Child Component: You should provide a child component to the AzureAD component, this will be rendered when login is successful
54
+
55
+
### Optional
56
+
57
+
-`authority`:A string representing your Azure Active Directory application policy. Includeif you are trying to authenticate against your Azure Active Directory application. If you're using a B2C AAD, it is usually in the format of: `https://login.microsoftonline.com/tfp/<your-tenant-name>.onmicrosoft.com/<your-sign-in-sign-up-policy>`
58
+
59
+
- `type`: `LoginType.Popup`. Popup is currently the only type available; redirect is currently buggy and disabled.
60
+
61
+
- `reduxStore`: If you want to use redux for auth, you can provide a redux store which the AzureAD component will dispatch a `AAD_LOGIN_SUCCESS` action, as well as a `payload` containing `IUserInfo`
62
+
63
+
## Login
29
64
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.
65
+
30
66
```javascript
31
67
import AzureAD from 'AzureAD'
32
68
33
69
loginCallback = (login) => {
34
70
return <button onclick={login}>Login</button>;
35
71
};
72
+
36
73
// ...
37
74
```
75
+
38
76
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.
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.
88
+
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/get-started-azure-ad) 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.
89
+
90
+
## Logout
46
91
47
-
### Logout
92
+
Logging out is just as easy.
48
93
49
-
Logging out is just as easy.
50
94
```javascript
51
95
logoutCallback= (logout) => {
52
-
return<button onclick={logout}>Logout</button>;
96
+
return (
97
+
<div>
98
+
You're logged in!
99
+
<button onclick={logout}>Logout</button>
100
+
</div>
101
+
);
53
102
};
54
103
```
104
+
55
105
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
106
57
-
###Samples
107
+
## Samples
58
108
59
109
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
110
61
-
###Integrating with a Redux Store
111
+
## Integrating with a Redux Store
62
112
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.
113
+
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
114
65
115
Import your store into the file rendering the AzureAD component and pass it in:
66
116
67
117
```javascript
68
118
<AzureAD
69
119
reduxStore={store}
70
120
clientID={'<Application ID for your application>'}
0 commit comments