forked from liupeirong/react-aspnetcore-graph-b2b-or-b2c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth-utils.js
58 lines (51 loc) · 1.58 KB
/
auth-utils.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
57
58
import { UserAgentApplication, Logger } from "msal";
import { env } from "./config";
export const requiresInteraction = errorMessage => {
if (!errorMessage || !errorMessage.length) {
return false;
}
return (
errorMessage.indexOf("consent_required") > -1 ||
errorMessage.indexOf("interaction_required") > -1 ||
errorMessage.indexOf("login_required") > -1
);
};
export const getAPI = async (url, accessToken) => {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
return response.json();
};
export const postAPI = async (url, accessToken, data) => {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`
},
body: JSON.stringify(data)
});
return "Status code: " + response.status;
};
export const msalApp = typeof window === 'undefined' ? null : new UserAgentApplication({
auth: {
clientId: env.auth.clientId,
authority: env.auth.authorityURL,
redirectUri: env.auth.redirectURL,
validateAuthority: (env.auth.validateAuthority === 'true'),
postLogoutRedirectUri: env.auth.redirectURL,
navigateToLoginRequestUrl: false
},
cache: {
cacheLocation: "sessionStorage",
storeAuthStateInCookie: false
},
system: {
navigateFrameWait: 500,
logger: new Logger((logLevel, message) => {
console.log(message);
}),
}
});