Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
dotCMS/core#19542 Fix the logout on SESSION_DESTROYED event from webs…
Browse files Browse the repository at this point in the history
…ocket (#1510)

* dotCMS/core#19542 Fix the logout on SESSION_DESTROYED event from websocket

* Fix test
  • Loading branch information
fmontes committed Nov 13, 2020
1 parent 6d674fb commit 56b9b8d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 51 deletions.
57 changes: 12 additions & 45 deletions projects/dotcms-js/src/lib/core/login.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { CoreWebService } from './core-web.service';
import { Injectable } from '@angular/core';
import { Observable, Subject, of } from 'rxjs';
import { Router } from '@angular/router';
import { LoggerService } from './logger.service';
import { HttpCode } from './util/http-code';
import { pluck, tap, map } from 'rxjs/operators';
Expand All @@ -18,6 +17,8 @@ export interface DotLoginParams {
backEndLogin: boolean;
}

export const LOGOUT_URL = '/dotAdmin/logout';

/**
* This Service get the server configuration to display in the login component
* and execute the login and forgot password routines
Expand All @@ -33,7 +34,6 @@ export class LoginService {
private urls: any;

constructor(
private router: Router,
private coreWebService: CoreWebService,
private dotcmsEventsService: DotcmsEventsService,
private loggerService: LoggerService
Expand All @@ -59,7 +59,7 @@ export class LoginService {
this.loggerService.debug('User Logged In Date: ', this.auth.user.loggedInDate);
// if the destroyed event happens after the logged in date, so proceed!
if (!this.auth.user.loggedInDate || this.isLogoutAfterLastLogin(date)) {
this.logOutUser().subscribe(() => {});
this.logOutUser();
}
});
}
Expand Down Expand Up @@ -215,7 +215,9 @@ export class LoginService {
this.setAuth(auth);
this.coreWebService
.subscribeToHttpError(HttpCode.UNAUTHORIZED)
.subscribe(() => this.logOutUser().subscribe(() => {}));
.subscribe(() => {
this.logOutUser();
});
return response.entity;
})
);
Expand Down Expand Up @@ -243,36 +245,6 @@ export class LoginService {
);
}

/**
* Call the logout rest api
* @returns Observable<any>
*/
public logOutUser(): Observable<any> {
return this.coreWebService
.requestView({
url: this.urls.logout
})
.pipe(
map((_response) => {
const nullAuth = {
loginAsUser: null,
user: null,
isLoginAs: false
};

this.loggerService.debug('Processing the logOutUser');
this.setAuth(nullAuth);

// on logout close the websocket
this.dotcmsEventsService.destroy();

this.loggerService.debug('Navigating to Public Login');

this.router.navigate(['/public/login']);
})
);
}

/**
* Executes the call to the recover passwrod rest api
* @param email User email address
Expand Down Expand Up @@ -321,8 +293,7 @@ export class LoginService {

private isLogoutAfterLastLogin(date): boolean {
return (
this.auth.user &&
this.auth.user.loggedInDate &&
this.auth?.user?.loggedInDate &&
date &&
Number(date) > Number(this.auth.user.loggedInDate)
);
Expand All @@ -344,16 +315,12 @@ export class LoginService {
}

/**
* Request and store the login as _auth list.
* Call the logout rest api
* @returns Observable<any>
*/
// private loadLoginAsUsersList(includeNUsers: boolean, filter: string): Observable<any> {
// return this.coreWebService
// .requestView({
// url: `${this.urls
// .loginAsUserList}?includeUsersCount=${includeNUsers}&filter=${filter}`
// })
// .pluck('entity');
// }
private logOutUser(): void {
window.location.href = LOGOUT_URL;
}
}

export interface User {
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/services/dot-router/dot-router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PortletNav } from '@models/navigation';
import { Subject } from 'rxjs';
import { DotAppsSites } from '@shared/models/dot-apps/dot-apps.model';
import { NavigationExtras } from '@angular/router';
import { LOGOUT_URL } from 'dotcms-js';

@Injectable()
export class DotRouterService {
Expand Down Expand Up @@ -116,7 +117,7 @@ export class DotRouterService {
* @memberof DotRouterService
*/
doLogOut(): void {
window.location.href = '/dotAdmin/logout';
window.location.href = LOGOUT_URL;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ describe('DotMyAccountComponent', () => {
spyOn<any>(accountService, 'updateUser').and.returnValue(
of({ entity: { reauthenticate: true } })
);
spyOn(loginService, 'logOutUser').and.returnValue(of({}));
spyOn(comp.close, 'emit');

const user = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h5 class="toolbar-user__user-name" id="dot-toolbar-user-name">
</li>
<li class="user-menu__item">
<a
href="/dotAdmin/logout"
[href]="logoutUrl"
*ngIf="!auth.loginAsUser"
id="dot-toolbar-user-link-logout"
[textContent]="'Logout' | dm"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, ViewChild, OnInit, Inject } from '@angular/core';
import { DotDropdownComponent } from '@components/_common/dot-dropdown-component/dot-dropdown.component';
import { IframeOverlayService } from '../../../_common/iframe/service/iframe-overlay.service';
import { LoginService, Auth, LoggerService } from 'dotcms-js';
import { LoginService, Auth, LoggerService, LOGOUT_URL } from 'dotcms-js';
import { LOCATION_TOKEN } from 'src/app/providers';
import { DotNavigationService } from '@components/dot-navigation/services/dot-navigation.service';
import { DotRouterService } from '@services/dot-router/dot-router.service';
Expand All @@ -18,6 +18,8 @@ export class DotToolbarUserComponent implements OnInit {
showLoginAs = false;
showMyAccount = false;

logoutUrl = LOGOUT_URL;

constructor(
@Inject(LOCATION_TOKEN) private location: Location,
private loggerService: LoggerService,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, ViewEncapsulation } from '@angular/core';
import { LoginService } from 'dotcms-js';
import { LoginService, LOGOUT_URL } from 'dotcms-js';
import { DotRouterService } from '../../../../api/services/dot-router/dot-router.service';
import { take } from 'rxjs/operators';

Expand All @@ -12,7 +12,7 @@ export class DotLogOutContainerComponent {
constructor(loginService: LoginService, router: DotRouterService) {
loginService.isLogin$.pipe(take(1)).subscribe((isLogin) => {
if (isLogin) {
window.location.href = '/dotAdmin/logout';
window.location.href = LOGOUT_URL;
} else {
router.goToLogin();
}
Expand Down

0 comments on commit 56b9b8d

Please sign in to comment.