@@ -104,11 +104,20 @@ You can test your application by signing in a user to the application then using
104
104
When these conditions are met , the app can extract the claims challenge from the API response header as follows :
105
105
106
106
```javascript
107
- const authenticateHeader = response .headers .get ('www-authenticate' );
108
- const claimsChallenge = parseChallenges (authenticateHeader ).claims ;
109
-
110
- // ...
107
+ try {
108
+ const response = await fetch (apiEndpoint , options );
109
+
110
+ if (response .status == = 401 && response .headers .get ('www-authenticate' )) {
111
+ const authenticateHeader = response .headers .get ('www-authenticate' );
112
+ const claimsChallenge = parseChallenges (authenticateHeader ).claims ;
113
+
114
+ // use the claims challenge to acquire a new access token...
115
+ }
116
+ } catch (error ) {
117
+ // ...
118
+ }
111
119
120
+ // helper function to parse the www-authenticate header
112
121
function parseChallenges (header ) {
113
122
const schemeSeparator = header .indexOf (' ' );
114
123
const challenges = header .substring (schemeSeparator + 1 ).split (',' );
@@ -126,24 +135,20 @@ function parseChallenges(header) {
126
135
Your app would then use the claims challenge to acquire a new access token for the resource .
127
136
128
137
```javascript
138
+ const tokenRequest = {
139
+ claims : window .atob (claimsChallenge ), // decode the base64 string
140
+ scopes : ['User.Read' ]
141
+ account : msalInstance .getActiveAccount ();
142
+ };
143
+
129
144
let tokenResponse ;
130
145
131
146
try {
132
- tokenResponse = await msalInstance .acquireTokenSilent ({
133
- claims : window .atob (claimsChallenge ), // decode the base64 string
134
- scopes : scopes , // e.g ['User.Read', 'Contacts.Read']
135
- account : account , // current active account
136
- });
137
-
147
+ tokenResponse = await msalInstance .acquireTokenSilent (tokenRequest );
138
148
} catch (error ) {
139
149
if (error instanceof InteractionRequiredAuthError ) {
140
- tokenResponse = await msalInstance .acquireTokenPopup ({
141
- claims : window .atob (claimsChallenge ), // decode the base64 string
142
- scopes : scopes , // e.g ['User.Read', 'Contacts.Read']
143
- account : account , // current active account
144
- });
150
+ tokenResponse = await msalInstance .acquireTokenPopup (tokenRequest );
145
151
}
146
-
147
152
}
148
153
```
149
154
@@ -154,8 +159,7 @@ const msalConfig = {
154
159
auth : {
155
160
clientId : 'Enter_the_Application_Id_Here' ,
156
161
clientCapabilities : [" CP1" ]
157
- // the remaining settings
158
- // ...
162
+ // remaining settings...
159
163
}
160
164
}
161
165
0 commit comments