From 26a743322e3294a5c090ab15a9a1308302b8f36d Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Wed, 4 Apr 2018 17:01:49 +0200 Subject: [PATCH] Added i18n label and fixes --- resources/i18n/en.json | 8 +++++ src/app/core/auth/auth.interceptor.ts | 2 +- src/app/core/auth/auth.service.ts | 33 ++++++++++++++++----- src/app/shared/log-in/log-in.component.html | 4 +-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/resources/i18n/en.json b/resources/i18n/en.json index c148b06565..6a08009f8a 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -165,5 +165,13 @@ "header": "Log out from DSpace", "submit": "Log out" } + }, + "auth": { + "messages": { + "expired": "Your session has expired. Please log in again." + }, + "errors": { + "invalid-user": "Invalid email or password." + } } } diff --git a/src/app/core/auth/auth.interceptor.ts b/src/app/core/auth/auth.interceptor.ts index 45c41312a1..ab105d825d 100644 --- a/src/app/core/auth/auth.interceptor.ts +++ b/src/app/core/auth/auth.interceptor.ts @@ -142,7 +142,7 @@ export class AuthInterceptor implements HttpInterceptor { } else if (this.isUnauthorized(error)) { // The access token provided is expired, revoked, malformed, or invalid for other reasons // Redirect to the login route - this.store.dispatch(new RedirectWhenTokenExpiredAction('Your session has expired. Please log in again.')); + this.store.dispatch(new RedirectWhenTokenExpiredAction('auth.messages.expired')); } } // Return error response as is. diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index 8d9605782a..cb827b1101 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -49,7 +49,7 @@ export class AuthService { // and is not the login route, clear redirect url and messages const routeUrlObs = this.store.select(routerStateSelector) .filter((routerState: RouterReducerState) => isNotUndefined(routerState) && isNotUndefined(routerState.state)) - .filter((routerState: RouterReducerState) => (routerState.state.url !== LOGIN_ROUTE)) + .filter((routerState: RouterReducerState) => !this.isLoginRoute(routerState.state.url)) .map((routerState: RouterReducerState) => routerState.state.url); const redirectUrlObs = this.getRedirectUrl(); routeUrlObs.pipe( @@ -57,10 +57,23 @@ export class AuthService { map(([routeUrl, redirectUrl]) => [routeUrl, redirectUrl]) ).filter(([routeUrl, redirectUrl]) => isNotEmpty(redirectUrl) && (routeUrl !== redirectUrl)) .subscribe(() => { - this.setRedirectUrl(undefined); + this.clearRedirectUrl(); }); } + /** + * Check if is a login page route + * + * @param {string} url + * @returns {Boolean}. + */ + protected isLoginRoute(url: string) { + const urlTree: UrlTree = this.router.parseUrl(url); + const g: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; + const segment = '/' + g.toString(); + return segment === LOGIN_ROUTE; + } + /** * Authenticate the user * @@ -175,7 +188,7 @@ export class AuthService { if (!status.authenticated) { return true; } else { - throw(new Error('Invalid email or password')); + throw(new Error('auth.errors.invalid-user')); } }) @@ -264,7 +277,7 @@ export class AuthService { */ public redirectToLogin() { // Hard redirect to login page, so that all state is definitely lost - this._window.nativeWindow.location.href = LOGIN_ROUTE; + this._window.nativeWindow.location.href = LOGIN_ROUTE + '?expired=true'; } /** @@ -275,9 +288,7 @@ export class AuthService { .first() .subscribe((redirectUrl) => { if (isNotEmpty(redirectUrl)) { - // Clear redirect url - this.setRedirectUrl(undefined); - this.storage.remove(REDIRECT_COOKIE); + this.clearRedirectUrl(); const urlTree: UrlTree = this.router.parseUrl(redirectUrl); const g: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; @@ -327,4 +338,12 @@ export class AuthService { this.storage.set(REDIRECT_COOKIE, url); this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(url) ? url : '')); } + + /** + * Clear redirect url + */ + clearRedirectUrl() { + this.store.dispatch(new SetRedirectUrlAction('')); + this.storage.remove(REDIRECT_COOKIE); + } } diff --git a/src/app/shared/log-in/log-in.component.html b/src/app/shared/log-in/log-in.component.html index d8d91ca271..0fe6c280de 100644 --- a/src/app/shared/log-in/log-in.component.html +++ b/src/app/shared/log-in/log-in.component.html @@ -17,8 +17,8 @@ formControlName="password" required type="password"> - - + + {{"login.form.new-user" | translate}}