Skip to content

Commit 39b9c42

Browse files
authored
Added Singout Pop-up API
Added Singout API Issue: dotnet#54756
1 parent 88ec3bc commit 39b9c42

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

src/Components/WebAssembly/Authentication.Msal/src/Interop/AuthenticationService.ts

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface AuthorizeService {
4343
completeSignOut(url: string): Promise<AuthenticationResult>;
4444
}
4545

46-
// These are the values for the .NET logger LogLevel.
46+
// These are the values for the .NET logger LogLevel.
4747
// We only use debug and trace
4848
export enum LogLevel {
4949
Trace = 0,
@@ -71,7 +71,7 @@ export class Logger {
7171
// Logs in the following format to keep consistency with the way ASP.NET Core logs to the console while avoiding the
7272
// additional overhead of passing the logger as a JSObjectReference
7373
// dbug: Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService[0]
74-
// <<message>>
74+
// <<message>>
7575
// trce: Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService[0]
7676
// <<message>>
7777
`${levelString}: Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService[0]
@@ -279,12 +279,12 @@ class MsalAuthorizeService implements AuthorizeService {
279279

280280
private async signInWithPopup(request: Msal.PopupRequest) {
281281
try {
282-
this.debug('Starting sign-in pop-up');
282+
this.debug('Starting sign-in pop-up.');
283283
return await this._msalApplication.loginPopup(request);
284284
} catch (e) {
285285
// If the user explicitly cancelled the pop-up, avoid performing a redirect.
286286
if (this.isMsalError(e) && e.errorCode !== Msal.BrowserAuthErrorMessage.userCancelledError.code) {
287-
this.debug('User canceled sign-in pop-up');
287+
this.debug('User canceled sign-in pop-up.');
288288
this.signInWithRedirect(request);
289289
} else {
290290
this.debug(`Sign-in pop-up failed: '${(e as Error).message}'.`);
@@ -327,15 +327,62 @@ class MsalAuthorizeService implements AuthorizeService {
327327
};
328328
this.trace('signOut-Request', request);
329329

330-
await this._msalApplication.logoutRedirect(request);
330+
const result = await this.signOutCore(request);
331+
this.trace('signOut-Response', result);
332+
if (!result) {
333+
return this.redirect();
334+
} else if (this.isMsalError(result)) {
335+
return this.error(result.errorMessage);
336+
}
331337

332-
// We are about to be redirected.
333-
return this.redirect();
338+
return this.success(state);
334339
} catch (e) {
340+
const message = (e as Error).message;
341+
this.debug(`Sign out error '${message}'`);
335342
return this.error((e as Error).message);
336343
}
337344
}
338345

346+
async signOutCore(request: Partial<Msal.EndSessionRequest>) : Promise<Msal.AuthenticationResult | Msal.AuthError | undefined> {
347+
this.trace('signOut-Request', request);
348+
const loginMode = this._settings.loginMode.toLowerCase();
349+
// shouldn't both calls await?
350+
if (loginMode == 'redirect') {
351+
return this.signOutWithRedirect(request as Msal.EndSessionRequest);
352+
} else {
353+
return this.signOutWithPopup(request as Msal.EndSessionPopupRequest);
354+
}
355+
}
356+
357+
private async signOutWithRedirect(request: Msal.EndSessionRequest) {
358+
try {
359+
this.debug('Starting sign-out redirect.');
360+
return await this._msalApplication.logoutRedirect(request)
361+
} catch (e) {
362+
this.debug(`Sing-out redirect failed: '${(e as Error).message}'`)
363+
return e as any;
364+
}
365+
}
366+
367+
private async signOutWithPopup(request: Msal.EndSessionPopupRequest) {
368+
try {
369+
this.debug('Starting sign-out pop-up.');
370+
return await this._msalApplication.logoutPopup(request);
371+
}
372+
catch (e) {
373+
// If the user explicitly cancelled the pop-up, avoid performing a redirect.
374+
if (this.isMsalError(e) && e.errorCode !== Msal.BrowserAuthErrorMessage.userCancelledError.code) {
375+
this.debug("User cancelled sign-out pop-up.");
376+
// shouldn't we await here? as well in signInWithPopup's fallback.
377+
this.signOutWithRedirect(request);
378+
}
379+
else {
380+
this.debug(`Sign-out pop-up failed: '${(e as Error).message}'`);
381+
return e as any;
382+
}
383+
}
384+
}
385+
339386
async completeSignOut(url: string) {
340387
this.trace('completeSignOut-request', url);
341388
try {

0 commit comments

Comments
 (0)