Skip to content

Commit

Permalink
Fixed usage of invalid command getAccount(). Should have been `getA…
Browse files Browse the repository at this point in the history
…llAccounts()[0]`.
  • Loading branch information
MatsAnd committed Mar 17, 2021
1 parent debe78c commit a17fea5
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/lib/auth-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export const MsalProvider = ({
setAuth({ ...copyAuth, authStatus: 'pending' })
pc.handleRedirectPromise().then((response) => {
if (response) {
const account = pc.getAllAccounts()[0]
saveUserdata(response, account)
const accounts = pc.getAllAccounts()
if (accounts && accounts.length > 0) saveUserdata(response, accounts[0])
} else {
const copyAuth = { ...auth }
setAuth({ ...copyAuth, authStatus: 'finished' })
Expand All @@ -103,13 +103,14 @@ export const MsalProvider = ({
setLoginError(error)
})

const accounts = pc.getAllAccounts()

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

const getTokenPopup = async (loginRequest) => {
const getTokenPopup = async loginRequest => {
try {
const response = await publicClient.acquireTokenSilent(loginRequest)
saveUserdata(response.accessToken, user)
Expand All @@ -194,7 +195,7 @@ export const MsalProvider = ({
}

// This function can be removed if you do not need to support IE
const getTokenRedirect = async (loginRequest) => {
const getTokenRedirect = async loginRequest => {
const copyAuth = { ...auth }
setAuth({ ...copyAuth, authStatus: 'pending' })
try {
Expand All @@ -212,12 +213,9 @@ export const MsalProvider = ({
}
}

const getToken = async (loginRequest) => {
if (isIE || isEdge) {
return await getTokenRedirect(loginRequest)
} else {
return await getTokenPopup(loginRequest)
}
const getToken = async loginRequest => {
if (isIE || isEdge) return await getTokenRedirect(loginRequest)
return await getTokenPopup(loginRequest)
}

// Implementerer api kall
Expand All @@ -232,8 +230,8 @@ export const MsalProvider = ({
return data
} catch (error) {
if (is401(error)) {
const account = publicClient.getAccount()
if (account) await updateToken(account)
const accounts = publicClient.getAllAccounts()
if (accounts && accounts.length > 0) await updateToken(accounts[0])

axios.defaults.headers.common.Authorization = `Bearer ${idToken}`
try {
Expand All @@ -250,8 +248,8 @@ export const MsalProvider = ({
}
} else {
console.warn('invalid or expired token')
const account = publicClient.getAccount()
if (account) await updateToken(account)
const accounts = publicClient.getAllAccounts()
if (accounts && accounts.length > 0) await updateToken(accounts[0])

return func()
}
Expand Down

0 comments on commit a17fea5

Please sign in to comment.