Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 6ea2a37

Browse files
authored
Merge pull request #5 from Azure-Samples/interceptor-error-handling
Show how to handle errors from the interceptor
2 parents 3963317 + a5849e1 commit 6ea2a37

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/app/profile/profile.component.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { MsalService } from '@azure/msal-angular';
33
import { HttpClient } from '@angular/common/http';
4+
import { InteractionRequiredAuthError, AuthError } from 'msal';
45

56
const GRAPH_ENDPOINT = 'https://graph.microsoft.com/v1.0/me';
67

@@ -19,9 +20,27 @@ export class ProfileComponent implements OnInit {
1920
}
2021

2122
getProfile() {
22-
this.http.get(GRAPH_ENDPOINT).toPromise()
23-
.then(profile => {
24-
this.profile = profile;
25-
});
23+
this.http.get(GRAPH_ENDPOINT)
24+
.subscribe({
25+
next: (profile) => {
26+
this.profile = profile;
27+
},
28+
error: (err: AuthError) => {
29+
// If there is an interaction required error,
30+
// call one of the interactive methods and then make the request again.
31+
if (InteractionRequiredAuthError.isInteractionRequiredError(err.errorCode)) {
32+
this.authService.acquireTokenPopup({
33+
scopes: this.authService.getScopesForEndpoint(GRAPH_ENDPOINT)
34+
})
35+
.then(() => {
36+
this.http.get(GRAPH_ENDPOINT)
37+
.toPromise()
38+
.then(profile => {
39+
this.profile = profile;
40+
});
41+
});
42+
}
43+
}
44+
});
2645
}
2746
}

0 commit comments

Comments
 (0)