Skip to content

Commit

Permalink
Merge 0ca7937 into 830c4a7
Browse files Browse the repository at this point in the history
  • Loading branch information
runely committed Mar 29, 2021
2 parents 830c4a7 + 0ca7937 commit a04db6c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,34 @@ const App = () => {
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
</ul>
</details>
- `apiPost` (function) - posts the provided data to the URL using the users id_token
<details>
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>data <i>(required)</i></ul>
<li>data <i>(required)</i>
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
</ul>
</details>
- `apiPut` (function) - updates/put the provided data to the URL using the users id_token
<details>
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>data <i>(required)</i></ul>
<li>data <i>(required)</i>
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
</ul>
</details>
- `apiDelete` (function) - deletes data from provided URL using the users id_token
<details>
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
</ul>
</details>

#### User object
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export default {
storeAuthStateInCookie: process.env.AUTH_STATE_IN_COOKIE === 'true' || false
},
userInfoUrl: process.env.AUTH_USER_INFO_URL || 'https://graph.microsoft.com/v1.0/me?$select=userPrincipalName,onPremisesSamaccountName,givenName,surname,displayName',
isMock: process.env.AUTH_IS_MOCK || process.env.REACT_APP_IS_MOCK || false,
isMock: (process.env.AUTH_IS_MOCK && process.env.AUTH_IS_MOCK === 'true') || (process.env.REACT_APP_IS_MOCK && process.env.REACT_APP_IS_MOCK === 'true') || false,
mockUser: process.env.AUTH_MOCK_USER ? JSON.parse(process.env.AUTH_MOCK_USER) : defaultMockUser
}
18 changes: 9 additions & 9 deletions src/lib/auth-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,21 @@ export const MsalProvider = ({
const is401 = error => /401/.test(error.message)
const isValid = (token, expires) => token && expires > new Date().getTime()

const retry = async func => {
const retry = async (func, returnFullResponse) => {
if (isValid(idToken, expires)) {
axios.defaults.headers.common.Authorization = `Bearer ${idToken}`
try {
const { data } = await func()
return data
const res = await func()
return !returnFullResponse ? res.data : res
} catch (error) {
if (is401(error)) {
const accounts = publicClient.getAllAccounts()
if (accounts && accounts.length > 0) await updateToken(accounts[0])

axios.defaults.headers.common.Authorization = `Bearer ${idToken}`
try {
const { data } = await func()
return data
const res = await func()
return !returnFullResponse ? res.data : res
} catch (error) {
console.error(error)
return false
Expand All @@ -255,10 +255,10 @@ export const MsalProvider = ({
}
}

const apiGet = url => retry(() => axios.get(url))
const apiPost = (url, payload) => retry(() => axios.post(url, payload))
const apiPut = (url, payload) => retry(() => axios.put(url, payload))
const apiDelete = url => retry(() => axios.delete(url))
const apiGet = (url, returnFullResponse = false) => retry(() => axios.get(url), returnFullResponse)
const apiPost = (url, payload, returnFullResponse = false) => retry(() => axios.post(url, payload), returnFullResponse)
const apiPut = (url, payload, returnFullResponse = false) => retry(() => axios.put(url, payload), returnFullResponse)
const apiDelete = (url, returnFullResponse = false) => retry(() => axios.delete(url), returnFullResponse)

return (
<MsalContext.Provider
Expand Down

0 comments on commit a04db6c

Please sign in to comment.