-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth-utils.js
65 lines (60 loc) · 1.63 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
59
60
61
62
63
64
65
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}`
}
});
if (response.status === 200) {
const responseJson = await response.json();
return responseJson;
} else {
throw new Error("fetch get failed: " + url);
}
};
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)
});
if (response.status >= 200) {
return { http_status: response.status };
} else {
throw new Error("fetch post failed: " + url);
}
};
export const msalApp = 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);
})
}
});