Skip to content

Commit

Permalink
Endret fra user til account, for å være konsekvent med begrepene, og …
Browse files Browse the repository at this point in the history
…byttet ut stedene user ble gitt med i updateToken til account (fra publicClient/pc)
  • Loading branch information
MatsAnd committed Mar 16, 2021
1 parent f09d1ad commit 36f244a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,42 +90,41 @@ const App = () => {
<details>
<summary>Parameters</summary>
<ul>
<li>options (object) - <a href="https://azuread.github.io/microsoft-authentication-library-for-js/ref/msal-browser/modules/_src_request_redirectrequest_.html#redirectrequest">loginRequest</a> <i>(required)</i></li>
<li>options (object) - <a href="https://azuread.github.io/microsoft-authentication-library-for-js/ref/modules/_azure_msal_browser.html#redirectrequest">loginRequest</a> <i>(required)</i></li>
<li>method (string): loginRedirect or loginPopup</ul>
</details>
- `logout` (function) - trigger logout (clears session storage and redirects to azure)
- `getToken` (function) - gathers and returns the users access token
<details>
<summary>Parameters</summary>
<ul>
<li>options (object) - <a href="https://azuread.github.io/microsoft-authentication-library-for-js/ref/msal-browser/modules/_src_request_redirectrequest_.html#redirectrequest">loginRequest</a> <i>(required)</i></li>
<li>method (string): loginRedirect or loginPopup</ul>
<li>options (object) - <a href="https://azuread.github.io/microsoft-authentication-library-for-js/ref/modules/_azure_msal_browser.html#redirectrequest">loginRequest</a> <i>(required)</i></li>
</details>
- `apiGet` (function) - gets data from provided URL using the users id_token
<details>
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>url (string) <i>(required)</i></li>
</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>url (string) <i>(required)</i></li>
<li>data <i>(required)</i></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>url (string) <i>(required)</i></li>
<li>data <i>(required)</i></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>url (string) <i>(required)</i></li>
</details>

#### User object
Expand Down
34 changes: 18 additions & 16 deletions src/lib/auth-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ export const MsalProvider = ({
})
}

async function updateToken (user) {
async function updateToken (account) {
if (!publicClient) return false
const response = await publicClient.acquireTokenSilent({ account: user.username, ...loginScopes })
await saveUserdata(response, user)
const response = await publicClient.acquireTokenSilent({ account, ...loginScopes })
await saveUserdata(response, account)
}

useEffect(() => {
Expand All @@ -90,8 +90,8 @@ export const MsalProvider = ({
setAuth({ ...copyAuth, authStatus: 'pending' })
pc.handleRedirectPromise().then((response) => {
if (response) {
const user = pc.getAllAccounts()[0]
saveUserdata(response, user)
const account = pc.getAllAccounts()[0]
saveUserdata(response, account)
} else {
const copyAuth = { ...auth }
setAuth({ ...copyAuth, authStatus: 'finished' })
Expand All @@ -105,11 +105,11 @@ export const MsalProvider = ({

// Dersom bruker er innlogget fra tidligere
if (pc.getAllAccounts().length > 0) {
const user = pc.getAllAccounts()[0]
const account = pc.getAllAccounts()[0]
const copyAuth = { ...auth }
setAuth({ ...copyAuth, authStatus: 'pending' })
if (!token) {
updateToken(user)
updateToken(account)
} else {
const copyAuth = { ...auth }
setAuth({ ...copyAuth, isAuthenticated: token && expires > new Date().getTime(), authStatus: 'finished' })
Expand Down Expand Up @@ -147,9 +147,8 @@ export const MsalProvider = ({
const copyAuth = { ...auth }
setAuth({ ...copyAuth, authStatus: 'pending' })
await publicClient.loginPopup(loginRequest)
if (publicClient.getAccount()) {
updateToken(publicClient.getAccount())
}
const account = publicClient.getAccount()
if (account) await updateToken(account)
} catch (error) {
console.error(error)
setLoginError(error)
Expand Down Expand Up @@ -213,9 +212,8 @@ export const MsalProvider = ({
}
}

const getToken = async (loginRequest, method) => {
const signInType = (isIE || isEdge) ? 'loginRedirect' : method
if (signInType === 'loginRedirect') {
const getToken = async (loginRequest) => {
if (isIE || isEdge) {
return await getTokenRedirect(loginRequest)
} else {
return await getTokenPopup(loginRequest)
Expand All @@ -234,7 +232,9 @@ export const MsalProvider = ({
return data
} catch (error) {
if (is401(error)) {
await updateToken(user)
const account = publicClient.getAccount()
if (account) await updateToken(account)

axios.defaults.headers.common.Authorization = `Bearer ${idToken}`
try {
const { data } = await func()
Expand All @@ -249,8 +249,10 @@ export const MsalProvider = ({
}
}
} else {
console.warn('invalid token or expire')
await updateToken(user)
console.warn('invalid or expired token')
const account = publicClient.getAccount()
if (account) await updateToken(account)

return func()
}
}
Expand Down

0 comments on commit 36f244a

Please sign in to comment.