-
Notifications
You must be signed in to change notification settings - Fork 0
/
azureAuth.js
56 lines (51 loc) · 2.04 KB
/
azureAuth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
signIn,
signOut,
msalApp,
acquireToken,
userAzureGraph,
biGraph
} from "./msalConfiguration";
import {
loginScope,
photoScope,
loginScopeBI
} from './scopeConfig';
export function* authUser() {
try {
const getUserAccount = yield msalApp.getAllAccounts();
if (getUserAccount.length === 0) {
yield signIn();
} else {
// ############################# User Auth agains Azure AD #############################
// acquireToken to Authenticate User in Azure AD.
const tokenResponse = yield acquireToken(loginScope);
// Returns a object, example:
console.log(
tokenResponse.idToken,
tokenResponse.accessToken,
tokenResponse.account.username,
tokenResponse.account.name,
tokenResponse.idTokenClaims.preferred_username,
tokenResponse.idTokenClaims.oid
)
// ############################# User PHOTO #############################
// acquireToken to read User Photo from Azure AD.
const tokenResponsePhoto = yield acquireToken(photoScope);
// Use token to get User Photo
const getUserData = yield userAzureGraph(tokenResponsePhoto.accessToken);
// Returns a object, example:
console.log(getUserData.userImage)
// ############################# Integration with other Azure services #############################
// acquireToken to Authenticate agains Power BI in Azure AD and read resource
const getBIAccess = yield acquireToken(loginScopeBI);
// Use token to get Power BI new Token, which can be used to direct integration
const getBIToken = yield biGraph(getBIAccess.accessToken);
// Return Power BI token, for Power BI access and integration, example
console.log(getBIToken)
}
} catch (error) {
yield signOut();
console.warn('Error: ', error)
}
}