mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 07:23:03 +00:00
intermediate commit
This commit is contained in:
@@ -1,13 +1,21 @@
|
||||
import { of as observableOf, Observable } from 'rxjs';
|
||||
import {
|
||||
take,
|
||||
filter,
|
||||
startWith,
|
||||
first,
|
||||
distinctUntilChanged,
|
||||
map,
|
||||
withLatestFrom
|
||||
} from 'rxjs/operators';
|
||||
import { Inject, Injectable } from '@angular/core';
|
||||
import { PRIMARY_OUTLET, Router, UrlSegmentGroup, UrlTree } from '@angular/router';
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { REQUEST } from '@nguniversal/express-engine/tokens';
|
||||
|
||||
import { RouterReducerState } from '@ngrx/router-store';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { select, Store } from '@ngrx/store';
|
||||
import { CookieAttributes } from 'js-cookie';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map, withLatestFrom } from 'rxjs/operators';
|
||||
|
||||
import { Eperson } from '../eperson/models/eperson.model';
|
||||
import { AuthRequestService } from './auth-request.service';
|
||||
@@ -17,7 +25,12 @@ import { AuthStatus } from './models/auth-status.model';
|
||||
import { AuthTokenInfo, TOKENITEM } from './models/auth-token-info.model';
|
||||
import { isEmpty, isNotEmpty, isNotNull, isNotUndefined } from '../../shared/empty.util';
|
||||
import { CookieService } from '../../shared/services/cookie.service';
|
||||
import { getAuthenticationToken, getRedirectUrl, isAuthenticated, isTokenRefreshing } from './selectors';
|
||||
import {
|
||||
getAuthenticationToken,
|
||||
getRedirectUrl,
|
||||
isAuthenticated,
|
||||
isTokenRefreshing
|
||||
} from './selectors';
|
||||
import { AppState, routerStateSelector } from '../../app.reducer';
|
||||
import { ResetAuthenticationMessagesAction, SetRedirectUrlAction } from './auth.actions';
|
||||
import { NativeWindowRef, NativeWindowService } from '../../shared/services/window.service';
|
||||
@@ -46,21 +59,24 @@ export class AuthService {
|
||||
protected router: Router,
|
||||
protected storage: CookieService,
|
||||
protected store: Store<AppState>) {
|
||||
this.store.select(isAuthenticated)
|
||||
.startWith(false)
|
||||
.subscribe((authenticated: boolean) => this._authenticated = authenticated);
|
||||
this.store.pipe(
|
||||
select(isAuthenticated),
|
||||
startWith(false)
|
||||
).subscribe((authenticated: boolean) => this._authenticated = authenticated);
|
||||
|
||||
// If current route is different from the one setted in authentication guard
|
||||
// and is not the login route, clear redirect url and messages
|
||||
const routeUrl$ = this.store.select(routerStateSelector)
|
||||
.filter((routerState: RouterReducerState) => isNotUndefined(routerState) && isNotUndefined(routerState.state))
|
||||
.filter((routerState: RouterReducerState) => !this.isLoginRoute(routerState.state.url))
|
||||
.map((routerState: RouterReducerState) => routerState.state.url);
|
||||
const redirectUrl$ = this.store.select(getRedirectUrl).distinctUntilChanged();
|
||||
const routeUrl$ = this.store.pipe(
|
||||
select(routerStateSelector),
|
||||
filter((routerState: RouterReducerState) => isNotUndefined(routerState) && isNotUndefined(routerState.state)),
|
||||
filter((routerState: RouterReducerState) => !this.isLoginRoute(routerState.state.url)),
|
||||
map((routerState: RouterReducerState) => routerState.state.url)
|
||||
);
|
||||
const redirectUrl$ = this.store.pipe(select(getRedirectUrl), distinctUntilChanged());
|
||||
routeUrl$.pipe(
|
||||
withLatestFrom(redirectUrl$),
|
||||
map(([routeUrl, redirectUrl]) => [routeUrl, redirectUrl])
|
||||
).filter(([routeUrl, redirectUrl]) => isNotEmpty(redirectUrl) && (routeUrl !== redirectUrl))
|
||||
).pipe(filter(([routeUrl, redirectUrl]) => isNotEmpty(redirectUrl) && (routeUrl !== redirectUrl)))
|
||||
.subscribe(() => {
|
||||
this.clearRedirectUrl();
|
||||
});
|
||||
@@ -93,14 +109,14 @@ export class AuthService {
|
||||
let headers = new HttpHeaders();
|
||||
headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
|
||||
options.headers = headers;
|
||||
return this.authRequestService.postToEndpoint('login', body, options)
|
||||
.map((status: AuthStatus) => {
|
||||
return this.authRequestService.postToEndpoint('login', body, options).pipe(
|
||||
map((status: AuthStatus) => {
|
||||
if (status.authenticated) {
|
||||
return status;
|
||||
} else {
|
||||
throw(new Error('Invalid email or password'));
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
@@ -109,7 +125,7 @@ export class AuthService {
|
||||
* @returns {Observable<boolean>}
|
||||
*/
|
||||
public isAuthenticated(): Observable<boolean> {
|
||||
return this.store.select(isAuthenticated);
|
||||
return this.store.pipe(select(isAuthenticated));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,14 +139,14 @@ export class AuthService {
|
||||
headers = headers.append('Accept', 'application/json');
|
||||
headers = headers.append('Authorization', `Bearer ${token.accessToken}`);
|
||||
options.headers = headers;
|
||||
return this.authRequestService.getRequest('status', options)
|
||||
.map((status: AuthStatus) => {
|
||||
return this.authRequestService.getRequest('status', options).pipe(
|
||||
map((status: AuthStatus) => {
|
||||
if (status.authenticated) {
|
||||
return status.eperson[0];
|
||||
} else {
|
||||
throw(new Error('Not authenticated'));
|
||||
}
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,9 +160,10 @@ export class AuthService {
|
||||
* Checks if token is present into storage and is not expired
|
||||
*/
|
||||
public hasValidAuthenticationToken(): Observable<AuthTokenInfo> {
|
||||
return this.store.select(getAuthenticationToken)
|
||||
.take(1)
|
||||
.map((authTokenInfo: AuthTokenInfo) => {
|
||||
return this.store.pipe(
|
||||
select(getAuthenticationToken),
|
||||
take(1),
|
||||
map((authTokenInfo: AuthTokenInfo) => {
|
||||
let token: AuthTokenInfo;
|
||||
// Retrieve authentication token info and check if is valid
|
||||
token = isNotEmpty(authTokenInfo) ? authTokenInfo : this.storage.get(TOKENITEM);
|
||||
@@ -155,7 +172,8 @@ export class AuthService {
|
||||
} else {
|
||||
throw false;
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,14 +185,14 @@ export class AuthService {
|
||||
headers = headers.append('Accept', 'application/json');
|
||||
headers = headers.append('Authorization', `Bearer ${token.accessToken}`);
|
||||
options.headers = headers;
|
||||
return this.authRequestService.postToEndpoint('login', {}, options)
|
||||
.map((status: AuthStatus) => {
|
||||
return this.authRequestService.postToEndpoint('login', {}, options).pipe(
|
||||
map((status: AuthStatus) => {
|
||||
if (status.authenticated) {
|
||||
return status.token;
|
||||
} else {
|
||||
throw(new Error('Not authenticated'));
|
||||
}
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,7 +211,7 @@ export class AuthService {
|
||||
// details and then return the new user object
|
||||
// but, let's just return the new user for this example.
|
||||
// this._authenticated = true;
|
||||
return Observable.of(user);
|
||||
return observableOf(user);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,15 +222,15 @@ export class AuthService {
|
||||
// Send a request that sign end the session
|
||||
let headers = new HttpHeaders();
|
||||
headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
|
||||
const options: HttpOptions = Object.create({headers, responseType: 'text'});
|
||||
return this.authRequestService.getRequest('logout', options)
|
||||
.map((status: AuthStatus) => {
|
||||
const options: HttpOptions = Object.create({ headers, responseType: 'text' });
|
||||
return this.authRequestService.getRequest('logout', options).pipe(
|
||||
map((status: AuthStatus) => {
|
||||
if (!status.authenticated) {
|
||||
return true;
|
||||
} else {
|
||||
throw(new Error('auth.errors.invalid-user'));
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
@@ -233,7 +251,7 @@ export class AuthService {
|
||||
*/
|
||||
public getToken(): AuthTokenInfo {
|
||||
let token: AuthTokenInfo;
|
||||
this.store.select(getAuthenticationToken)
|
||||
this.store.pipe(select(getAuthenticationToken))
|
||||
.subscribe((authTokenInfo: AuthTokenInfo) => {
|
||||
// Retrieve authentication token info and check if is valid
|
||||
token = authTokenInfo || null;
|
||||
@@ -246,9 +264,10 @@ export class AuthService {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public isTokenExpiring(): Observable<boolean> {
|
||||
return this.store.select(isTokenRefreshing)
|
||||
.first()
|
||||
.map((isRefreshing: boolean) => {
|
||||
return this.store.pipe(
|
||||
select(isTokenRefreshing),
|
||||
first(),
|
||||
map((isRefreshing: boolean) => {
|
||||
if (this.isTokenExpired() || isRefreshing) {
|
||||
return false;
|
||||
} else {
|
||||
@@ -256,6 +275,7 @@ export class AuthService {
|
||||
return token.expires - (60 * 5 * 1000) < Date.now();
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -279,7 +299,7 @@ export class AuthService {
|
||||
|
||||
// Set the cookie expire date
|
||||
const expires = new Date(expireDate);
|
||||
const options: CookieAttributes = {expires: expires};
|
||||
const options: CookieAttributes = { expires: expires };
|
||||
|
||||
// Save cookie with the token
|
||||
return this.storage.set(TOKENITEM, token, options);
|
||||
@@ -324,8 +344,8 @@ export class AuthService {
|
||||
* Redirect to the route navigated before the login
|
||||
*/
|
||||
public redirectToPreviousUrl() {
|
||||
this.getRedirectUrl()
|
||||
.first()
|
||||
this.getRedirectUrl().pipe(
|
||||
first())
|
||||
.subscribe((redirectUrl) => {
|
||||
if (isNotEmpty(redirectUrl)) {
|
||||
this.clearRedirectUrl();
|
||||
@@ -359,9 +379,9 @@ export class AuthService {
|
||||
getRedirectUrl(): Observable<string> {
|
||||
const redirectUrl = this.storage.get(REDIRECT_COOKIE);
|
||||
if (isNotEmpty(redirectUrl)) {
|
||||
return Observable.of(redirectUrl);
|
||||
return observableOf(redirectUrl);
|
||||
} else {
|
||||
return this.store.select(getRedirectUrl);
|
||||
return this.store.pipe(select(getRedirectUrl));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -374,7 +394,7 @@ export class AuthService {
|
||||
|
||||
// Set the cookie expire date
|
||||
const expires = new Date(expireDate);
|
||||
const options: CookieAttributes = {expires: expires};
|
||||
const options: CookieAttributes = { expires: expires };
|
||||
this.storage.set(REDIRECT_COOKIE, url, options);
|
||||
this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(url) ? url : ''));
|
||||
}
|
||||
|
Reference in New Issue
Block a user