Skip to content

Commit

Permalink
Fixed close account. Reuse sign-out on account close. (#1285)
Browse files Browse the repository at this point in the history
* Fixed close account. Reuse signout on account close.
  • Loading branch information
ygrik authored Apr 27, 2021
1 parent e394f10 commit 79ee33d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum AadEndpoints {
legacy = "login.windows.net"
}

export const closeAccount = "close-account";
export const hashSignOut = "signout";
export const pageUrlSignIn = "/signin";
export const pageUrlSignInSso = "/signinsso";
Expand Down
44 changes: 25 additions & 19 deletions src/routing/signOutRouteGuard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RouteGuard, Route } from "@paperbits/common/routing";
import { IAuthenticator } from "../authentication";
import { hashSignOut } from "../constants";
import * as Constants from "../constants";
import { MapiClient } from "../services/mapiClient";
import { Identity } from "../contracts/identity";
import { TenantService } from "../services/tenantService";
Expand All @@ -16,35 +16,41 @@ export class SignOutRouteGuard implements RouteGuard {
) { }

public async canActivate(route: Route): Promise<boolean> {
if (route.hash !== hashSignOut) {
if (route.hash !== Constants.hashSignOut) {
return true;
}

const isDelegationEnabled = await this.tenantService.isDelegationEnabled();
const isSignOutAfterClose = sessionStorage.getItem(Constants.closeAccount);

if (isDelegationEnabled) {
const token = await this.authenticator.getAccessTokenAsString();
if (isSignOutAfterClose !== "true") {
const isDelegationEnabled = await this.tenantService.isDelegationEnabled();

if (token) {
try {
const identity = await this.mapiClient.get<Identity>("/identity", [MapiClient.getPortalHeader("delegationSignOut")]);
if (isDelegationEnabled) {
const token = await this.authenticator.getAccessTokenAsString();

if (identity) {
const redirectUrl = await this.backendService.getDelegationUrl(DelegationAction.signOut, { userId: identity.id });
if (redirectUrl) {
window.open(redirectUrl, "_self");
if (token) {
try {
const identity = await this.mapiClient.get<Identity>("/identity", [MapiClient.getPortalHeader("delegationSignOut")]);

if (identity) {
const redirectUrl = await this.backendService.getDelegationUrl(DelegationAction.signOut, { userId: identity.id });
if (redirectUrl) {
window.open(redirectUrl, "_self");
}
}
}
}
catch (error) {
const errorMessage: string = error.message;
const requestedUrl: string = error.requestedUrl;
if (errorMessage.startsWith("Could not complete the request.") && requestedUrl.endsWith("/delegation-url")) {
alert("Delegation CORS error: self-hosted portal and Dev portal must have the same domain");
catch (error) {
const errorMessage: string = error.message;
const requestedUrl: string = error.requestedUrl;
if (errorMessage.startsWith("Could not complete the request.") && requestedUrl.endsWith("/delegation-url")) {
alert("Delegation CORS error: self-hosted portal and Dev portal must have the same domain");
}
return true;
}
return true;
}
}
} else {
sessionStorage.removeItem(Constants.closeAccount);
}

this.authenticator.clearAccessToken();
Expand Down
4 changes: 2 additions & 2 deletions src/services/usersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ export class UsersService {

await this.mapiClient.delete<string>(query, [header, MapiClient.getPortalHeader("deleteUser")]);

this.authenticator.clearAccessToken();
location.assign("/");
sessionStorage.setItem(Constants.closeAccount, "true");
this.signOut();
}
catch (error) {
this.navigateToSignin();
Expand Down

0 comments on commit 79ee33d

Please sign in to comment.