Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return full response #26

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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