Skip to content

Commit

Permalink
Merge pull request #17 from hncleary/ios-cache-fix
Browse files Browse the repository at this point in the history
Fix Multiple Accounts in Cache Issue iOS
  • Loading branch information
bartwesselink committed Mar 1, 2023
2 parents fb5385f + 397323d commit 7302db5
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class MsAuthPlugin: CAPPlugin {
if let error = error {
print("Unable to logout: \(error)")

call.reject("Unable to lgout")
call.reject("Unable to logout")

return
}
Expand Down Expand Up @@ -132,6 +132,33 @@ public class MsAuthPlugin: CAPPlugin {
let msalParameters = MSALParameters()
msalParameters.completionBlockQueue = DispatchQueue.main

// Check through multiple accounts in the cache if present
do {
let accounts = try applicationContext.allAccounts() // Get all cached accounts
if accounts.count > 1 {
let authorityUrl = applicationContext.configuration.authority.url
for account in accounts {
if let tenants = account.tenantProfiles {
for tenant in tenants {
if let tenantId = tenant.tenantId {
// Find first account where authority url matches tenant id
if authorityUrl.absoluteString.contains(tenantId) {
completion(account)
return
}
}
}
}
}
// If no match is found for the authority url (fallback for multi-tenant app registration)
completion(accounts[0]) // return the first available account
return
}
} catch {
print("Unable to access cached accounts list")
}


applicationContext.getCurrentAccount(with: msalParameters, completionBlock: { (currentAccount, _, error) in
if let error = error {
print("Unable to query current account: \(error)")
Expand All @@ -149,6 +176,7 @@ public class MsAuthPlugin: CAPPlugin {

completion(nil)
})

}

func acquireTokenInteractively(applicationContext: MSALPublicClientApplication, scopes: [String], completion: @escaping (MSALResult?) -> Void) {
Expand Down

0 comments on commit 7302db5

Please sign in to comment.