diff --git a/projects/auth/src/lib/auth-form/auth-form.component.html b/projects/auth/src/lib/auth-form/auth-form.component.html index 07cb4d7ac1..5fc7d2dcb4 100644 --- a/projects/auth/src/lib/auth-form/auth-form.component.html +++ b/projects/auth/src/lib/auth-form/auth-form.component.html @@ -1,7 +1,7 @@
-
+ - diff --git a/projects/auth/src/lib/auth-form/auth-form.component.ts b/projects/auth/src/lib/auth-form/auth-form.component.ts index be4b9ecd18..a2cde9103c 100644 --- a/projects/auth/src/lib/auth-form/auth-form.component.ts +++ b/projects/auth/src/lib/auth-form/auth-form.component.ts @@ -5,7 +5,8 @@ import { Input, Optional } from '@angular/core'; -import { Router } from '@angular/router'; +import { Router, NavigationStart } from '@angular/router'; +import { filter } from 'rxjs/operators'; import { ConfigService } from '@igo2/core'; import { AuthOptions } from '../shared/auth.interface'; @@ -40,6 +41,7 @@ export class AuthFormComponent implements OnInit { public user; public visible = true; + public logoutDiv = false; constructor( public auth: AuthService, @@ -48,47 +50,74 @@ export class AuthFormComponent implements OnInit { ) { this.options = this.config.getConfig('auth') || {}; this.visible = Object.getOwnPropertyNames(this.options).length !== 0; - - if (this.auth.decodeToken()) { - this.user = { - name: this.auth.decodeToken().user.sourceId - }; - } } public ngOnInit() { this.analyzeRoute(); + this.getName(); } public login() { this.auth.goToRedirectUrl(); + this.getName(); } public logout() { this.auth.logout().subscribe(() => { - if (this.router && this.options.loginRoute) { - this.router.navigate([this.options.loginRoute]); + this.user = undefined; + if (this.router) { + if (this.options.logoutRoute) { + this.router.navigate([this.options.logoutRoute]); + } else if (this.options.homeRoute) { + this.router.navigate([this.options.homeRoute]); + } } }); } + public home() { + if (this.router && this.options.homeRoute) { + this.router.navigate([this.options.homeRoute]); + } + } + + private getName() { + if (this.auth.decodeToken()) { + const tokenDecoded = this.auth.decodeToken(); + this.user = { + name: tokenDecoded.user.firstName || tokenDecoded.user.sourceId + }; + } + } + private analyzeRoute() { if (!this.router) { return; } - const logoutRoute = this.options.logoutRoute; - const loginRoute = this.options.loginRoute; - const currentRoute = this.router.url; + this.router.events + .pipe(filter(event => event instanceof NavigationStart)) + .subscribe((changeEvent: any) => { + if (changeEvent.url) { + const currentRoute = changeEvent.url; + const logoutRoute = this.options.logoutRoute; + const loginRoute = this.options.loginRoute; - const isLogoutRoute: boolean = currentRoute === logoutRoute; - const isLoginRoute: boolean = currentRoute === loginRoute; + const isLogoutRoute: boolean = currentRoute === logoutRoute; + const isLoginRoute: boolean = currentRoute === loginRoute; - if (isLogoutRoute) { - this.logout(); - } else if (isLoginRoute) { - this.backgroundDisable = false; - this.alreadyConnectedDiv = true; - } + this.backgroundDisable = true; + this.logoutDiv = false; + + if (isLogoutRoute) { + this.auth.logout(); + this.backgroundDisable = false; + this.logoutDiv = true; + } else if (isLoginRoute) { + this.backgroundDisable = false; + this.alreadyConnectedDiv = true; + } + } + }); } } diff --git a/projects/auth/src/lib/shared/auth.interface.ts b/projects/auth/src/lib/shared/auth.interface.ts index 6414ceb360..c99e09fec4 100644 --- a/projects/auth/src/lib/shared/auth.interface.ts +++ b/projects/auth/src/lib/shared/auth.interface.ts @@ -19,6 +19,7 @@ export interface AuthOptions { allowAnonymous?: boolean; loginRoute?: string; logoutRoute?: string; + homeRoute?: string; intern?: AuthInternOptions; facebook?: AuthFacebookOptions; google?: AuthGoogleOptions; diff --git a/projects/auth/src/lib/shared/profils.guard.ts b/projects/auth/src/lib/shared/profils.guard.ts index 2a739065ca..338c0559f0 100644 --- a/projects/auth/src/lib/shared/profils.guard.ts +++ b/projects/auth/src/lib/shared/profils.guard.ts @@ -5,6 +5,7 @@ import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; +import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { ConfigService } from '@igo2/core'; diff --git a/projects/auth/src/locale/en.auth.json b/projects/auth/src/locale/en.auth.json index 04803296cf..fc25d5f800 100644 --- a/projects/auth/src/locale/en.auth.json +++ b/projects/auth/src/locale/en.auth.json @@ -7,7 +7,9 @@ "password": "Password", "signOut": "Sign out", "user": "User", - "welcome": "Welcome {{name}}!" + "welcome": "Welcome {{name}}!", + "deconnection": "You are logged out.", + "home": "Return to the home page" } } } diff --git a/projects/auth/src/locale/fr.auth.json b/projects/auth/src/locale/fr.auth.json index b8f8eb6ee3..146f0e6989 100644 --- a/projects/auth/src/locale/fr.auth.json +++ b/projects/auth/src/locale/fr.auth.json @@ -7,7 +7,9 @@ "password": "Mot de passe", "signOut": "Se déconnecter", "user": "Utilisateur", - "welcome": "Bienvenue {{name}}!" + "welcome": "Bienvenue {{name}}!", + "deconnection": "Vous êtes déconnecté", + "home": "Retourner à la page d'accueil" } } } diff --git a/projects/auth/src/public_api.ts b/projects/auth/src/public_api.ts index b2d2764455..4baf407358 100644 --- a/projects/auth/src/public_api.ts +++ b/projects/auth/src/public_api.ts @@ -4,7 +4,10 @@ export { AuthFormComponent } from './lib/auth-form'; export * from './lib/shared/auth.service'; +export * from './lib/shared/logged.guard'; export * from './lib/shared/auth.guard'; +export * from './lib/shared/admin.guard'; +export * from './lib/shared/profils.guard'; export * from './lib/shared/auth.interceptor'; export * from './lib/shared/auth.interface'; export * from './lib/shared/protected.directive';