Skip to content

Commit af37c5f

Browse files
author
Francisco Arturo Cruz Zelante
committed
Merge branch 'EDATOS-3338' into 'develop'
EDATOS-3338 - Autenticación en área personal See merge request istac/edatos-external-users!62
2 parents be1dd0f + ee20262 commit af37c5f

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

edatos-external-users-external-js/webapp/src/app/app-routing.module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RouterModule, Routes } from '@angular/router';
33
import { USER, ALL_ALLOWED, FILTER_ROLES } from './core/service';
44
import { UserRouteAccessGuard } from './core/guard';
55
import { DEFAULT_PATH } from './app.constants';
6+
import { NotLoggedUserCanAccessGuard } from './core/guard/not-logged-user-can-access.guard';
67

78
const APP_ROUTES: Routes = [
89
{

edatos-external-users-external-js/webapp/src/app/app.constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const ERROR_ALERT_KEY = 'alert-errors';
1616
export const LANG_KEY = 'lang';
1717
export const DEFAULT_LANG = 'es';
1818

19-
export const DEFAULT_PATH = '/login';
19+
export const DEFAULT_PATH = '/filter';
2020

2121
export const ITEMS_PER_PAGE = 20;
2222
export const PAGINATION_OPTIONS = [5, 10, 15, 20];

edatos-external-users-external-js/webapp/src/app/core/core.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { UserRouteAccessGuard } from './guard';
2020
import localeEs from '@angular/common/locales/es';
2121
import { registerLocaleData } from '@angular/common';
2222
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
23+
import { NotLoggedUserCanAccessGuard } from './guard/not-logged-user-can-access.guard';
2324

2425
registerLocaleData(localeEs, 'es');
2526
@NgModule({
@@ -33,6 +34,7 @@ registerLocaleData(localeEs, 'es');
3334
StateStorageService,
3435
AccountUserService,
3536
UserRouteAccessGuard,
37+
NotLoggedUserCanAccessGuard,
3638
PageTitleService,
3739
Title,
3840
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, Data, CanLoad, Route } from '@angular/router';
3+
import { DEFAULT_PATH } from '@app/app.constants';
4+
import { Observable } from 'rxjs';
5+
import { map } from 'rxjs/operators';
6+
import { Principal } from '../service/auth/principal.service';
7+
8+
@Injectable()
9+
export class NotLoggedUserCanAccessGuard implements CanLoad, CanActivate {
10+
constructor(private router: Router, private principal: Principal) {}
11+
12+
canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
13+
return this.canNavigateIsNotAuthenticated();
14+
}
15+
16+
canLoad(route: Route): Promise<boolean> {
17+
return this.canNavigateIsNotAuthenticated();
18+
}
19+
20+
private checkIsAuthenticated(): Promise<boolean> {
21+
return this.principal.identity().then((account) => {
22+
return !!account;
23+
});
24+
}
25+
26+
private canNavigateIsNotAuthenticated(): Promise<boolean> {
27+
return this.checkIsAuthenticated().then((authenticated) => {
28+
if (authenticated) {
29+
this.router.navigate([DEFAULT_PATH]);
30+
return false;
31+
}
32+
return true;
33+
});
34+
}
35+
}

edatos-external-users-external-js/webapp/src/app/core/service/auth/login.service.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ import { Injectable } from '@angular/core';
22

33
import { Principal } from './principal.service';
44
import { AuthServerProvider } from './auth-jwt.service';
5-
import { ConfigService } from '@app/config/config.service';
5+
import { Observable } from 'rxjs';
6+
import { Router } from '@angular/router';
67

78
@Injectable()
89
export class LoginService {
9-
constructor(private principal: Principal, private authServerProvider: AuthServerProvider, private configService: ConfigService) {}
10+
constructor(private router: Router, private principal: Principal, private authServerProvider: AuthServerProvider) {}
1011

1112
logout() {
12-
this.authServerProvider.logout().subscribe();
13-
this.principal.authenticate(null);
13+
return this.authServerProvider
14+
.logout()
15+
.toPromise()
16+
.then(() => {
17+
this.principal.authenticate(null);
18+
this.router.navigate(['login']);
19+
});
1420
}
15-
1621
}

edatos-external-users-external-js/webapp/src/app/layouts/navbar/navbar.component.ts

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export class NavbarComponent implements OnInit {
4545
logout() {
4646
this.collapseNavbar();
4747
this.loginService.logout();
48-
const config = this.configService.getConfig();
4948
}
5049

5150
toggleNavbar() {

edatos-external-users-external-js/webapp/src/app/modules/login/login-routing.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
33
import { LoginComponent } from './login.component';
44
import { ALL_ALLOWED } from '@app/core/service/auth';
5+
import { NotLoggedUserCanAccessGuard } from '@app/core/guard/not-logged-user-can-access.guard';
56

67
export const loginRoutes: Routes = [
78
{
89
path: '',
910
component: LoginComponent,
1011
data: {
11-
roles: ALL_ALLOWED,
1212
pageTitle: 'login.home.title',
1313
},
14+
canActivate: [NotLoggedUserCanAccessGuard],
1415
},
1516
];
1617

0 commit comments

Comments
 (0)