Skip to content

Commit

Permalink
Merge pull request #30 from recognizegroup/feature/add-logout-all-imp…
Browse files Browse the repository at this point in the history
…lementation

Add logout all implementation
  • Loading branch information
wslaghekke committed Sep 15, 2023
2 parents 16bce5a + ee5d0d6 commit cfc7815
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public void onError(@NonNull MsalException ex) {
}
}

@PluginMethod
public void logoutAll(final PluginCall call) {
logout(call);
}

protected String getAuthorityUrl(ISingleAccountPublicClientApplication context) {
return context.getConfiguration().getDefaultAuthority().getAuthorityURL().toString();
}
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
CAP_PLUGIN(MsAuthPlugin, "MsAuthPlugin",
CAP_PLUGIN_METHOD(login, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(logout, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(logoutAll, CAPPluginReturnPromise);
)
45 changes: 45 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,51 @@ public class MsAuthPlugin: CAPPlugin {
}
}
}

@objc func logoutAll(_ call: CAPPluginCall) {
guard let context = createContextFromPluginCall(call) else {
call.reject("Unable to create context, check logs")
return
}

guard let bridgeViewController = bridge?.viewController else {
call.reject("Unable to get Capacitor bridge.viewController")
return
}

do {
let accounts = try context.allAccounts()
var completed = 0

accounts.forEach {
let wvParameters = MSALWebviewParameters(authPresentationViewController: bridgeViewController)
let signoutParameters = MSALSignoutParameters(webviewParameters: wvParameters)
signoutParameters.signoutFromBrowser = false // set this to true if you also want to signout from browser or webview

context.signout(with: $0, signoutParameters: signoutParameters, completionBlock: {(_, error) in
completed += 1

if let error = error {
print("Unable to logout: \(error)")

call.reject("Unable to logout")

return
}

if completed == accounts.count {
call.resolve()
}
})
}
} catch {
print("Unable to logout: \(error)")

call.reject("Unable to logout")

return
}
}

private func createContextFromPluginCall(_ call: CAPPluginCall) -> MSALPublicClientApplication? {
guard let clientId = call.getString("clientId") else {
Expand Down
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export type LogoutOptions = BaseOptions;
export interface MsAuthPlugin {
login(options: LoginOptions): Promise<{ accessToken: string; idToken: string; scopes: string[] }>;
logout(options: LogoutOptions): Promise<void>;
logoutAll(options: LogoutOptions): Promise<void>;
}
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class MsAuth extends WebPlugin implements MsAuthPlugin {
}
}

logoutAll(options: WebLogoutOptions): Promise<void> {
return this.logout(options);
}

private createContext(options: WebBaseOptions) {
const config = {
auth: {
Expand Down

0 comments on commit cfc7815

Please sign in to comment.