From 36f244acdddd880b65fbe403084207e9edfb0761 Mon Sep 17 00:00:00 2001 From: Mats Andreassen Date: Tue, 16 Mar 2021 16:01:44 +0100 Subject: [PATCH] =?UTF-8?q?Endret=20fra=20user=20til=20account,=20for=20?= =?UTF-8?q?=C3=A5=20v=C3=A6re=20konsekvent=20med=20begrepene,=20og=20bytte?= =?UTF-8?q?t=20ut=20stedene=20user=20ble=20gitt=20med=20i=20updateToken=20?= =?UTF-8?q?til=20account=20(fra=20publicClient/pc)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 ++++++------- src/lib/auth-provider.js | 34 ++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 32c4849..c4df9a7 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ const App = () => {
Parameters
- `logout` (function) - trigger logout (clears session storage and redirects to azure) @@ -98,34 +98,33 @@ const App = () => {
Parameters +
  • options (object) - loginRequest (required)
  • - `apiGet` (function) - gets data from provided URL using the users id_token
    Parameters
    - `apiPost` (function) - posts the provided data to the URL using the users id_token
    Parameters
    - `apiPut` (function) - updates/put the provided data to the URL using the users id_token
    Parameters
    - `apiDelete` (function) - deletes data from provided URL using the users id_token
    Parameters
    #### User object diff --git a/src/lib/auth-provider.js b/src/lib/auth-provider.js index d6b1817..af00827 100644 --- a/src/lib/auth-provider.js +++ b/src/lib/auth-provider.js @@ -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(() => { @@ -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' }) @@ -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' }) @@ -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) @@ -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) @@ -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() @@ -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() } }