mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Added action to retrieve authenticated eperson object
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import { Observable, of as observableOf, throwError as observableThrowError } from 'rxjs';
|
import { Observable, of as observableOf, throwError as observableThrowError } from 'rxjs';
|
||||||
import { distinctUntilChanged, filter, map, mergeMap, tap } from 'rxjs/operators';
|
import { distinctUntilChanged, filter, map, mergeMap, tap } from 'rxjs/operators';
|
||||||
import { Inject, Injectable } from '@angular/core';
|
import { Inject, Injectable } from '@angular/core';
|
||||||
import { EPersonDataService } from '../eperson/eperson-data.service';
|
|
||||||
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
||||||
import { RequestService } from '../data/request.service';
|
import { RequestService } from '../data/request.service';
|
||||||
import { GLOBAL_CONFIG } from '../../../config';
|
import { GLOBAL_CONFIG } from '../../../config';
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
// import @ngrx
|
// import @ngrx
|
||||||
import { Action } from '@ngrx/store';
|
import { Action } from '@ngrx/store';
|
||||||
|
|
||||||
// import type function
|
// import type function
|
||||||
import { type } from '../../shared/ngrx/type';
|
import { type } from '../../shared/ngrx/type';
|
||||||
|
|
||||||
// import models
|
// import models
|
||||||
import { EPerson } from '../eperson/models/eperson.model';
|
import { EPerson } from '../eperson/models/eperson.model';
|
||||||
import { AuthTokenInfo } from './models/auth-token-info.model';
|
import { AuthTokenInfo } from './models/auth-token-info.model';
|
||||||
@@ -31,6 +29,9 @@ export const AuthActionTypes = {
|
|||||||
REGISTRATION_ERROR: type('dspace/auth/REGISTRATION_ERROR'),
|
REGISTRATION_ERROR: type('dspace/auth/REGISTRATION_ERROR'),
|
||||||
REGISTRATION_SUCCESS: type('dspace/auth/REGISTRATION_SUCCESS'),
|
REGISTRATION_SUCCESS: type('dspace/auth/REGISTRATION_SUCCESS'),
|
||||||
SET_REDIRECT_URL: type('dspace/auth/SET_REDIRECT_URL'),
|
SET_REDIRECT_URL: type('dspace/auth/SET_REDIRECT_URL'),
|
||||||
|
RETRIEVE_AUTHENTICATED_EPERSON: type('dspace/auth/RETRIEVE_AUTHENTICATED_EPERSON'),
|
||||||
|
RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS: type('dspace/auth/RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS'),
|
||||||
|
RETRIEVE_AUTHENTICATED_EPERSON_ERROR: type('dspace/auth/RETRIEVE_AUTHENTICATED_EPERSON_ERROR'),
|
||||||
};
|
};
|
||||||
|
|
||||||
/* tslint:disable:max-classes-per-file */
|
/* tslint:disable:max-classes-per-file */
|
||||||
@@ -76,11 +77,11 @@ export class AuthenticatedSuccessAction implements Action {
|
|||||||
payload: {
|
payload: {
|
||||||
authenticated: boolean;
|
authenticated: boolean;
|
||||||
authToken: AuthTokenInfo;
|
authToken: AuthTokenInfo;
|
||||||
user: EPerson
|
userHref: string
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(authenticated: boolean, authToken: AuthTokenInfo, user: EPerson) {
|
constructor(authenticated: boolean, authToken: AuthTokenInfo, userHref: string) {
|
||||||
this.payload = { authenticated, authToken, user };
|
this.payload = { authenticated, authToken, userHref };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,6 +323,47 @@ export class SetRedirectUrlAction implements Action {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the authenticated eperson.
|
||||||
|
* @class RetrieveAuthenticatedEpersonAction
|
||||||
|
* @implements {Action}
|
||||||
|
*/
|
||||||
|
export class RetrieveAuthenticatedEpersonAction implements Action {
|
||||||
|
public type: string = AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON;
|
||||||
|
payload: string;
|
||||||
|
|
||||||
|
constructor(user: string) {
|
||||||
|
this.payload = user ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the authenticated eperson in the state.
|
||||||
|
* @class RetrieveAuthenticatedEpersonSuccessAction
|
||||||
|
* @implements {Action}
|
||||||
|
*/
|
||||||
|
export class RetrieveAuthenticatedEpersonSuccessAction implements Action {
|
||||||
|
public type: string = AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS;
|
||||||
|
payload: EPerson;
|
||||||
|
|
||||||
|
constructor(user: EPerson) {
|
||||||
|
this.payload = user ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the authenticated eperson in the state.
|
||||||
|
* @class RetrieveAuthenticatedEpersonSuccessAction
|
||||||
|
* @implements {Action}
|
||||||
|
*/
|
||||||
|
export class RetrieveAuthenticatedEpersonErrorAction implements Action {
|
||||||
|
public type: string = AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON_ERROR;
|
||||||
|
payload: Error;
|
||||||
|
|
||||||
|
constructor(payload: Error) {
|
||||||
|
this.payload = payload ;
|
||||||
|
}
|
||||||
|
}
|
||||||
/* tslint:enable:max-classes-per-file */
|
/* tslint:enable:max-classes-per-file */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -343,4 +385,8 @@ export type AuthActions
|
|||||||
| RegistrationErrorAction
|
| RegistrationErrorAction
|
||||||
| RegistrationSuccessAction
|
| RegistrationSuccessAction
|
||||||
| AddAuthenticationMessageAction
|
| AddAuthenticationMessageAction
|
||||||
| ResetAuthenticationMessagesAction;
|
| ResetAuthenticationMessagesAction
|
||||||
|
| RetrieveAuthenticatedEpersonAction
|
||||||
|
| RetrieveAuthenticatedEpersonErrorAction
|
||||||
|
| RetrieveAuthenticatedEpersonSuccessAction
|
||||||
|
| SetRedirectUrlAction;
|
||||||
|
@@ -18,12 +18,14 @@ import {
|
|||||||
LogOutErrorAction,
|
LogOutErrorAction,
|
||||||
LogOutSuccessAction,
|
LogOutSuccessAction,
|
||||||
RefreshTokenErrorAction,
|
RefreshTokenErrorAction,
|
||||||
RefreshTokenSuccessAction
|
RefreshTokenSuccessAction,
|
||||||
|
RetrieveAuthenticatedEpersonAction,
|
||||||
|
RetrieveAuthenticatedEpersonErrorAction,
|
||||||
|
RetrieveAuthenticatedEpersonSuccessAction
|
||||||
} from './auth.actions';
|
} from './auth.actions';
|
||||||
import { AuthServiceStub } from '../../shared/testing/auth-service-stub';
|
import { AuthServiceStub } from '../../shared/testing/auth-service-stub';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { AuthState } from './auth.reducer';
|
import { AuthState } from './auth.reducer';
|
||||||
|
|
||||||
import { EPersonMock } from '../../shared/testing/eperson-mock';
|
import { EPersonMock } from '../../shared/testing/eperson-mock';
|
||||||
|
|
||||||
describe('AuthEffects', () => {
|
describe('AuthEffects', () => {
|
||||||
@@ -42,13 +44,14 @@ describe('AuthEffects', () => {
|
|||||||
authServiceStub = new AuthServiceStub();
|
authServiceStub = new AuthServiceStub();
|
||||||
token = authServiceStub.getToken();
|
token = authServiceStub.getToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
init();
|
init();
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
providers: [
|
providers: [
|
||||||
AuthEffects,
|
AuthEffects,
|
||||||
{provide: AuthService, useValue: authServiceStub},
|
{ provide: AuthService, useValue: authServiceStub },
|
||||||
{provide: Store, useValue: store},
|
{ provide: Store, useValue: store },
|
||||||
provideMockActions(() => actions),
|
provideMockActions(() => actions),
|
||||||
// other providers
|
// other providers
|
||||||
],
|
],
|
||||||
@@ -63,11 +66,11 @@ describe('AuthEffects', () => {
|
|||||||
actions = hot('--a-', {
|
actions = hot('--a-', {
|
||||||
a: {
|
a: {
|
||||||
type: AuthActionTypes.AUTHENTICATE,
|
type: AuthActionTypes.AUTHENTICATE,
|
||||||
payload: {email: 'user', password: 'password'}
|
payload: { email: 'user', password: 'password' }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new AuthenticationSuccessAction(token)});
|
const expected = cold('--b-', { b: new AuthenticationSuccessAction(token) });
|
||||||
|
|
||||||
expect(authEffects.authenticate$).toBeObservable(expected);
|
expect(authEffects.authenticate$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
@@ -80,11 +83,11 @@ describe('AuthEffects', () => {
|
|||||||
actions = hot('--a-', {
|
actions = hot('--a-', {
|
||||||
a: {
|
a: {
|
||||||
type: AuthActionTypes.AUTHENTICATE,
|
type: AuthActionTypes.AUTHENTICATE,
|
||||||
payload: {email: 'user', password: 'wrongpassword'}
|
payload: { email: 'user', password: 'wrongpassword' }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new AuthenticationErrorAction(new Error('Message Error test'))});
|
const expected = cold('--b-', { b: new AuthenticationErrorAction(new Error('Message Error test')) });
|
||||||
|
|
||||||
expect(authEffects.authenticate$).toBeObservable(expected);
|
expect(authEffects.authenticate$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
@@ -94,9 +97,9 @@ describe('AuthEffects', () => {
|
|||||||
describe('authenticateSuccess$', () => {
|
describe('authenticateSuccess$', () => {
|
||||||
|
|
||||||
it('should return a AUTHENTICATED action in response to a AUTHENTICATE_SUCCESS action', () => {
|
it('should return a AUTHENTICATED action in response to a AUTHENTICATE_SUCCESS action', () => {
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.AUTHENTICATE_SUCCESS, payload: token}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.AUTHENTICATE_SUCCESS, payload: token } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new AuthenticatedAction(token)});
|
const expected = cold('--b-', { b: new AuthenticatedAction(token) });
|
||||||
|
|
||||||
expect(authEffects.authenticateSuccess$).toBeObservable(expected);
|
expect(authEffects.authenticateSuccess$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
@@ -106,9 +109,9 @@ describe('AuthEffects', () => {
|
|||||||
|
|
||||||
describe('when token is valid', () => {
|
describe('when token is valid', () => {
|
||||||
it('should return a AUTHENTICATED_SUCCESS action in response to a AUTHENTICATED action', () => {
|
it('should return a AUTHENTICATED_SUCCESS action in response to a AUTHENTICATED action', () => {
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.AUTHENTICATED, payload: token}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.AUTHENTICATED, payload: token } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new AuthenticatedSuccessAction(true, token, EPersonMock)});
|
const expected = cold('--b-', { b: new AuthenticatedSuccessAction(true, token, EPersonMock._links.self.href) });
|
||||||
|
|
||||||
expect(authEffects.authenticated$).toBeObservable(expected);
|
expect(authEffects.authenticated$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
@@ -118,23 +121,42 @@ describe('AuthEffects', () => {
|
|||||||
it('should return a AUTHENTICATED_ERROR action in response to a AUTHENTICATED action', () => {
|
it('should return a AUTHENTICATED_ERROR action in response to a AUTHENTICATED action', () => {
|
||||||
spyOn((authEffects as any).authService, 'authenticatedUser').and.returnValue(observableThrow(new Error('Message Error test')));
|
spyOn((authEffects as any).authService, 'authenticatedUser').and.returnValue(observableThrow(new Error('Message Error test')));
|
||||||
|
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.AUTHENTICATED, payload: token}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.AUTHENTICATED, payload: token } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new AuthenticatedErrorAction(new Error('Message Error test'))});
|
const expected = cold('--b-', { b: new AuthenticatedErrorAction(new Error('Message Error test')) });
|
||||||
|
|
||||||
expect(authEffects.authenticated$).toBeObservable(expected);
|
expect(authEffects.authenticated$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('authenticatedSuccess$', () => {
|
||||||
|
|
||||||
|
it('should return a RETRIEVE_AUTHENTICATED_EPERSON action in response to a AUTHENTICATED_SUCCESS action', () => {
|
||||||
|
actions = hot('--a-', {
|
||||||
|
a: {
|
||||||
|
type: AuthActionTypes.AUTHENTICATED_SUCCESS, payload: {
|
||||||
|
authenticated: true,
|
||||||
|
authToken: token,
|
||||||
|
userHref: EPersonMock._links.self.href
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const expected = cold('--b-', { b: new RetrieveAuthenticatedEpersonAction(EPersonMock._links.self.href) });
|
||||||
|
|
||||||
|
expect(authEffects.authenticatedSuccess$).toBeObservable(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('checkToken$', () => {
|
describe('checkToken$', () => {
|
||||||
|
|
||||||
describe('when check token succeeded', () => {
|
describe('when check token succeeded', () => {
|
||||||
it('should return a AUTHENTICATED action in response to a CHECK_AUTHENTICATION_TOKEN action', () => {
|
it('should return a AUTHENTICATED action in response to a CHECK_AUTHENTICATION_TOKEN action', () => {
|
||||||
|
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new AuthenticatedAction(token)});
|
const expected = cold('--b-', { b: new AuthenticatedAction(token) });
|
||||||
|
|
||||||
expect(authEffects.checkToken$).toBeObservable(expected);
|
expect(authEffects.checkToken$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
@@ -144,23 +166,53 @@ describe('AuthEffects', () => {
|
|||||||
it('should return a CHECK_AUTHENTICATION_TOKEN_ERROR action in response to a CHECK_AUTHENTICATION_TOKEN action', () => {
|
it('should return a CHECK_AUTHENTICATION_TOKEN_ERROR action in response to a CHECK_AUTHENTICATION_TOKEN action', () => {
|
||||||
spyOn((authEffects as any).authService, 'hasValidAuthenticationToken').and.returnValue(observableThrow(''));
|
spyOn((authEffects as any).authService, 'hasValidAuthenticationToken').and.returnValue(observableThrow(''));
|
||||||
|
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN, payload: token}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN, payload: token } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new CheckAuthenticationTokenErrorAction()});
|
const expected = cold('--b-', { b: new CheckAuthenticationTokenErrorAction() });
|
||||||
|
|
||||||
expect(authEffects.checkToken$).toBeObservable(expected);
|
expect(authEffects.checkToken$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('retrieveAuthenticatedEperson$', () => {
|
||||||
|
|
||||||
|
describe('when request is successful', () => {
|
||||||
|
it('should return a RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS action in response to a RETRIEVE_AUTHENTICATED_EPERSON action', () => {
|
||||||
|
actions = hot('--a-', {
|
||||||
|
a: {
|
||||||
|
type: AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON,
|
||||||
|
payload: EPersonMock._links.self.href
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const expected = cold('--b-', { b: new RetrieveAuthenticatedEpersonSuccessAction(EPersonMock) });
|
||||||
|
|
||||||
|
expect(authEffects.retrieveAuthenticatedEperson$).toBeObservable(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when request is not successful', () => {
|
||||||
|
it('should return a RETRIEVE_AUTHENTICATED_EPERSON_ERROR action in response to a RETRIEVE_AUTHENTICATED_EPERSON action', () => {
|
||||||
|
spyOn((authEffects as any).authService, 'retrieveAuthenticatedUserByHref').and.returnValue(observableThrow(new Error('Message Error test')));
|
||||||
|
|
||||||
|
actions = hot('--a-', { a: { type: AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON, payload: token } });
|
||||||
|
|
||||||
|
const expected = cold('--b-', { b: new RetrieveAuthenticatedEpersonErrorAction(new Error('Message Error test')) });
|
||||||
|
|
||||||
|
expect(authEffects.retrieveAuthenticatedEperson$).toBeObservable(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('refreshToken$', () => {
|
describe('refreshToken$', () => {
|
||||||
|
|
||||||
describe('when refresh token succeeded', () => {
|
describe('when refresh token succeeded', () => {
|
||||||
it('should return a REFRESH_TOKEN_SUCCESS action in response to a REFRESH_TOKEN action', () => {
|
it('should return a REFRESH_TOKEN_SUCCESS action in response to a REFRESH_TOKEN action', () => {
|
||||||
|
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.REFRESH_TOKEN}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.REFRESH_TOKEN } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new RefreshTokenSuccessAction(token)});
|
const expected = cold('--b-', { b: new RefreshTokenSuccessAction(token) });
|
||||||
|
|
||||||
expect(authEffects.refreshToken$).toBeObservable(expected);
|
expect(authEffects.refreshToken$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
@@ -170,9 +222,9 @@ describe('AuthEffects', () => {
|
|||||||
it('should return a REFRESH_TOKEN_ERROR action in response to a REFRESH_TOKEN action', () => {
|
it('should return a REFRESH_TOKEN_ERROR action in response to a REFRESH_TOKEN action', () => {
|
||||||
spyOn((authEffects as any).authService, 'refreshAuthenticationToken').and.returnValue(observableThrow(''));
|
spyOn((authEffects as any).authService, 'refreshAuthenticationToken').and.returnValue(observableThrow(''));
|
||||||
|
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.REFRESH_TOKEN, payload: token}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.REFRESH_TOKEN, payload: token } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new RefreshTokenErrorAction()});
|
const expected = cold('--b-', { b: new RefreshTokenErrorAction() });
|
||||||
|
|
||||||
expect(authEffects.refreshToken$).toBeObservable(expected);
|
expect(authEffects.refreshToken$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
@@ -184,9 +236,9 @@ describe('AuthEffects', () => {
|
|||||||
describe('when refresh token succeeded', () => {
|
describe('when refresh token succeeded', () => {
|
||||||
it('should return a LOG_OUT_SUCCESS action in response to a LOG_OUT action', () => {
|
it('should return a LOG_OUT_SUCCESS action in response to a LOG_OUT action', () => {
|
||||||
|
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.LOG_OUT}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.LOG_OUT } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new LogOutSuccessAction()});
|
const expected = cold('--b-', { b: new LogOutSuccessAction() });
|
||||||
|
|
||||||
expect(authEffects.logOut$).toBeObservable(expected);
|
expect(authEffects.logOut$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
@@ -196,9 +248,9 @@ describe('AuthEffects', () => {
|
|||||||
it('should return a REFRESH_TOKEN_ERROR action in response to a LOG_OUT action', () => {
|
it('should return a REFRESH_TOKEN_ERROR action in response to a LOG_OUT action', () => {
|
||||||
spyOn((authEffects as any).authService, 'logout').and.returnValue(observableThrow(new Error('Message Error test')));
|
spyOn((authEffects as any).authService, 'logout').and.returnValue(observableThrow(new Error('Message Error test')));
|
||||||
|
|
||||||
actions = hot('--a-', {a: {type: AuthActionTypes.LOG_OUT, payload: token}});
|
actions = hot('--a-', { a: { type: AuthActionTypes.LOG_OUT, payload: token } });
|
||||||
|
|
||||||
const expected = cold('--b-', {b: new LogOutErrorAction(new Error('Message Error test'))});
|
const expected = cold('--b-', { b: new LogOutErrorAction(new Error('Message Error test')) });
|
||||||
|
|
||||||
expect(authEffects.logOut$).toBeObservable(expected);
|
expect(authEffects.logOut$).toBeObservable(expected);
|
||||||
});
|
});
|
||||||
|
@@ -26,7 +26,10 @@ import {
|
|||||||
RefreshTokenSuccessAction,
|
RefreshTokenSuccessAction,
|
||||||
RegistrationAction,
|
RegistrationAction,
|
||||||
RegistrationErrorAction,
|
RegistrationErrorAction,
|
||||||
RegistrationSuccessAction
|
RegistrationSuccessAction,
|
||||||
|
RetrieveAuthenticatedEpersonAction,
|
||||||
|
RetrieveAuthenticatedEpersonErrorAction,
|
||||||
|
RetrieveAuthenticatedEpersonSuccessAction
|
||||||
} from './auth.actions';
|
} from './auth.actions';
|
||||||
import { EPerson } from '../eperson/models/eperson.model';
|
import { EPerson } from '../eperson/models/eperson.model';
|
||||||
import { AuthStatus } from './models/auth-status.model';
|
import { AuthStatus } from './models/auth-status.model';
|
||||||
@@ -66,11 +69,17 @@ export class AuthEffects {
|
|||||||
ofType(AuthActionTypes.AUTHENTICATED),
|
ofType(AuthActionTypes.AUTHENTICATED),
|
||||||
switchMap((action: AuthenticatedAction) => {
|
switchMap((action: AuthenticatedAction) => {
|
||||||
return this.authService.authenticatedUser(action.payload).pipe(
|
return this.authService.authenticatedUser(action.payload).pipe(
|
||||||
map((user: EPerson) => new AuthenticatedSuccessAction((user !== null), action.payload, user)),
|
map((userHref: string) => new AuthenticatedSuccessAction((userHref !== null), action.payload, userHref)),
|
||||||
catchError((error) => observableOf(new AuthenticatedErrorAction(error))),);
|
catchError((error) => observableOf(new AuthenticatedErrorAction(error))),);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@Effect()
|
||||||
|
public authenticatedSuccess$: Observable<Action> = this.actions$.pipe(
|
||||||
|
ofType(AuthActionTypes.AUTHENTICATED_SUCCESS),
|
||||||
|
map((action: AuthenticatedSuccessAction) => new RetrieveAuthenticatedEpersonAction(action.payload.userHref))
|
||||||
|
);
|
||||||
|
|
||||||
// It means "reacts to this action but don't send another"
|
// It means "reacts to this action but don't send another"
|
||||||
@Effect({ dispatch: false })
|
@Effect({ dispatch: false })
|
||||||
public authenticatedError$: Observable<Action> = this.actions$.pipe(
|
public authenticatedError$: Observable<Action> = this.actions$.pipe(
|
||||||
@@ -78,6 +87,16 @@ export class AuthEffects {
|
|||||||
tap((action: LogOutSuccessAction) => this.authService.removeToken())
|
tap((action: LogOutSuccessAction) => this.authService.removeToken())
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@Effect()
|
||||||
|
public retrieveAuthenticatedEperson$: Observable<Action> = this.actions$.pipe(
|
||||||
|
ofType(AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON),
|
||||||
|
switchMap((action: RetrieveAuthenticatedEpersonAction) => {
|
||||||
|
return this.authService.retrieveAuthenticatedUserByHref(action.payload).pipe(
|
||||||
|
map((user: EPerson) => new RetrieveAuthenticatedEpersonSuccessAction(user)),
|
||||||
|
catchError((error) => observableOf(new RetrieveAuthenticatedEpersonErrorAction(error))));
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
@Effect()
|
@Effect()
|
||||||
public checkToken$: Observable<Action> = this.actions$.pipe(ofType(AuthActionTypes.CHECK_AUTHENTICATION_TOKEN),
|
public checkToken$: Observable<Action> = this.actions$.pipe(ofType(AuthActionTypes.CHECK_AUTHENTICATION_TOKEN),
|
||||||
switchMap(() => {
|
switchMap(() => {
|
||||||
|
@@ -17,7 +17,7 @@ import {
|
|||||||
RefreshTokenAction,
|
RefreshTokenAction,
|
||||||
RefreshTokenErrorAction,
|
RefreshTokenErrorAction,
|
||||||
RefreshTokenSuccessAction,
|
RefreshTokenSuccessAction,
|
||||||
ResetAuthenticationMessagesAction,
|
ResetAuthenticationMessagesAction, RetrieveAuthenticatedEpersonErrorAction, RetrieveAuthenticatedEpersonSuccessAction,
|
||||||
SetRedirectUrlAction
|
SetRedirectUrlAction
|
||||||
} from './auth.actions';
|
} from './auth.actions';
|
||||||
import { AuthTokenInfo } from './models/auth-token-info.model';
|
import { AuthTokenInfo } from './models/auth-token-info.model';
|
||||||
@@ -107,16 +107,15 @@ describe('authReducer', () => {
|
|||||||
loading: true,
|
loading: true,
|
||||||
info: undefined
|
info: undefined
|
||||||
};
|
};
|
||||||
const action = new AuthenticatedSuccessAction(true, mockTokenInfo, EPersonMock);
|
const action = new AuthenticatedSuccessAction(true, mockTokenInfo, EPersonMock._links.self.href);
|
||||||
const newState = authReducer(initialState, action);
|
const newState = authReducer(initialState, action);
|
||||||
state = {
|
state = {
|
||||||
authenticated: true,
|
authenticated: true,
|
||||||
authToken: mockTokenInfo,
|
authToken: mockTokenInfo,
|
||||||
loaded: true,
|
loaded: false,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
loading: false,
|
loading: true,
|
||||||
info: undefined,
|
info: undefined
|
||||||
user: EPersonMock
|
|
||||||
};
|
};
|
||||||
expect(newState).toEqual(state);
|
expect(newState).toEqual(state);
|
||||||
});
|
});
|
||||||
@@ -242,6 +241,50 @@ describe('authReducer', () => {
|
|||||||
expect(newState).toEqual(state);
|
expect(newState).toEqual(state);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should properly set the state, in response to a RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS action', () => {
|
||||||
|
initialState = {
|
||||||
|
authenticated: true,
|
||||||
|
authToken: mockTokenInfo,
|
||||||
|
loaded: false,
|
||||||
|
error: undefined,
|
||||||
|
loading: true,
|
||||||
|
info: undefined
|
||||||
|
};
|
||||||
|
const action = new RetrieveAuthenticatedEpersonSuccessAction(EPersonMock);
|
||||||
|
const newState = authReducer(initialState, action);
|
||||||
|
state = {
|
||||||
|
authenticated: true,
|
||||||
|
authToken: mockTokenInfo,
|
||||||
|
loaded: true,
|
||||||
|
error: undefined,
|
||||||
|
loading: false,
|
||||||
|
info: undefined,
|
||||||
|
user: EPersonMock
|
||||||
|
};
|
||||||
|
expect(newState).toEqual(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should properly set the state, in response to a RETRIEVE_AUTHENTICATED_EPERSON_ERROR action', () => {
|
||||||
|
initialState = {
|
||||||
|
authenticated: false,
|
||||||
|
loaded: false,
|
||||||
|
error: undefined,
|
||||||
|
loading: true,
|
||||||
|
info: undefined
|
||||||
|
};
|
||||||
|
const action = new RetrieveAuthenticatedEpersonErrorAction(mockError);
|
||||||
|
const newState = authReducer(initialState, action);
|
||||||
|
state = {
|
||||||
|
authenticated: false,
|
||||||
|
authToken: undefined,
|
||||||
|
error: 'Test error message',
|
||||||
|
loaded: true,
|
||||||
|
loading: false,
|
||||||
|
info: undefined
|
||||||
|
};
|
||||||
|
expect(newState).toEqual(state);
|
||||||
|
});
|
||||||
|
|
||||||
it('should properly set the state, in response to a REFRESH_TOKEN action', () => {
|
it('should properly set the state, in response to a REFRESH_TOKEN action', () => {
|
||||||
initialState = {
|
initialState = {
|
||||||
authenticated: true,
|
authenticated: true,
|
||||||
|
@@ -8,7 +8,7 @@ import {
|
|||||||
LogOutErrorAction,
|
LogOutErrorAction,
|
||||||
RedirectWhenAuthenticationIsRequiredAction,
|
RedirectWhenAuthenticationIsRequiredAction,
|
||||||
RedirectWhenTokenExpiredAction,
|
RedirectWhenTokenExpiredAction,
|
||||||
RefreshTokenSuccessAction,
|
RefreshTokenSuccessAction, RetrieveAuthenticatedEpersonSuccessAction,
|
||||||
SetRedirectUrlAction
|
SetRedirectUrlAction
|
||||||
} from './auth.actions';
|
} from './auth.actions';
|
||||||
// import models
|
// import models
|
||||||
@@ -80,6 +80,7 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
});
|
});
|
||||||
|
|
||||||
case AuthActionTypes.AUTHENTICATED_ERROR:
|
case AuthActionTypes.AUTHENTICATED_ERROR:
|
||||||
|
case AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON_ERROR:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
authToken: undefined,
|
authToken: undefined,
|
||||||
@@ -91,12 +92,16 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
case AuthActionTypes.AUTHENTICATED_SUCCESS:
|
case AuthActionTypes.AUTHENTICATED_SUCCESS:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
authenticated: true,
|
authenticated: true,
|
||||||
authToken: (action as AuthenticatedSuccessAction).payload.authToken,
|
authToken: (action as AuthenticatedSuccessAction).payload.authToken
|
||||||
|
});
|
||||||
|
|
||||||
|
case AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS:
|
||||||
|
return Object.assign({}, state, {
|
||||||
loaded: true,
|
loaded: true,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
loading: false,
|
loading: false,
|
||||||
info: undefined,
|
info: undefined,
|
||||||
user: (action as AuthenticatedSuccessAction).payload.user
|
user: (action as RetrieveAuthenticatedEpersonSuccessAction).payload
|
||||||
});
|
});
|
||||||
|
|
||||||
case AuthActionTypes.AUTHENTICATE_ERROR:
|
case AuthActionTypes.AUTHENTICATE_ERROR:
|
||||||
|
@@ -5,14 +5,12 @@ import { ActivatedRoute, Router } from '@angular/router';
|
|||||||
import { Store, StoreModule } from '@ngrx/store';
|
import { Store, StoreModule } from '@ngrx/store';
|
||||||
import { REQUEST } from '@nguniversal/express-engine/tokens';
|
import { REQUEST } from '@nguniversal/express-engine/tokens';
|
||||||
import { of as observableOf } from 'rxjs';
|
import { of as observableOf } from 'rxjs';
|
||||||
import { LinkService } from '../cache/builders/link.service';
|
|
||||||
|
|
||||||
import { authReducer, AuthState } from './auth.reducer';
|
import { authReducer, AuthState } from './auth.reducer';
|
||||||
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
|
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { RouterStub } from '../../shared/testing/router-stub';
|
import { RouterStub } from '../../shared/testing/router-stub';
|
||||||
import { ActivatedRouteStub } from '../../shared/testing/active-router-stub';
|
import { ActivatedRouteStub } from '../../shared/testing/active-router-stub';
|
||||||
|
|
||||||
import { CookieService } from '../services/cookie.service';
|
import { CookieService } from '../services/cookie.service';
|
||||||
import { AuthRequestServiceStub } from '../../shared/testing/auth-request-service-stub';
|
import { AuthRequestServiceStub } from '../../shared/testing/auth-request-service-stub';
|
||||||
import { AuthRequestService } from './auth-request.service';
|
import { AuthRequestService } from './auth-request.service';
|
||||||
@@ -23,12 +21,21 @@ import { EPersonMock } from '../../shared/testing/eperson-mock';
|
|||||||
import { AppState } from '../../app.reducer';
|
import { AppState } from '../../app.reducer';
|
||||||
import { ClientCookieService } from '../services/client-cookie.service';
|
import { ClientCookieService } from '../services/client-cookie.service';
|
||||||
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
||||||
import { getMockRemoteDataBuildService } from '../../shared/mocks/mock-remote-data-build.service';
|
|
||||||
import { routeServiceStub } from '../../shared/testing/route-service-stub';
|
import { routeServiceStub } from '../../shared/testing/route-service-stub';
|
||||||
import { RouteService } from '../services/route.service';
|
import { RouteService } from '../services/route.service';
|
||||||
|
import { Observable } from 'rxjs/internal/Observable';
|
||||||
|
import { RemoteData } from '../data/remote-data';
|
||||||
|
import { createSuccessfulRemoteDataObject$ } from '../../shared/testing/utils';
|
||||||
|
import { EPersonDataService } from '../eperson/eperson-data.service';
|
||||||
|
|
||||||
describe('AuthService test', () => {
|
describe('AuthService test', () => {
|
||||||
|
|
||||||
|
const mockEpersonDataService: any = {
|
||||||
|
findByHref(href: string): Observable<RemoteData<EPerson>> {
|
||||||
|
return createSuccessfulRemoteDataObject$(EPersonMock);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let mockStore: Store<AuthState>;
|
let mockStore: Store<AuthState>;
|
||||||
let authService: AuthService;
|
let authService: AuthService;
|
||||||
let routeServiceMock: RouteService;
|
let routeServiceMock: RouteService;
|
||||||
@@ -62,7 +69,7 @@ describe('AuthService test', () => {
|
|||||||
linkService = {
|
linkService = {
|
||||||
resolveLinks: {}
|
resolveLinks: {}
|
||||||
};
|
};
|
||||||
spyOn(linkService, 'resolveLinks').and.returnValue({authenticated: true, eperson: observableOf({payload: {}})});
|
spyOn(linkService, 'resolveLinks').and.returnValue({ authenticated: true, eperson: observableOf({ payload: {} }) });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +90,7 @@ describe('AuthService test', () => {
|
|||||||
{ provide: RouteService, useValue: routeServiceStub },
|
{ provide: RouteService, useValue: routeServiceStub },
|
||||||
{ provide: ActivatedRoute, useValue: routeStub },
|
{ provide: ActivatedRoute, useValue: routeStub },
|
||||||
{ provide: Store, useValue: mockStore },
|
{ provide: Store, useValue: mockStore },
|
||||||
{ provide: LinkService, useValue: linkService },
|
{ provide: EPersonDataService, useValue: mockEpersonDataService },
|
||||||
CookieService,
|
CookieService,
|
||||||
AuthService
|
AuthService
|
||||||
],
|
],
|
||||||
@@ -101,8 +108,14 @@ describe('AuthService test', () => {
|
|||||||
expect(authService.authenticate.bind(null, 'user', 'passwordwrong')).toThrow();
|
expect(authService.authenticate.bind(null, 'user', 'passwordwrong')).toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the authenticated user object when user token is valid', () => {
|
it('should return the authenticated user href when user token is valid', () => {
|
||||||
authService.authenticatedUser(new AuthTokenInfo('test_token')).subscribe((user: EPerson) => {
|
authService.authenticatedUser(new AuthTokenInfo('test_token')).subscribe((userHref: string) => {
|
||||||
|
expect(userHref).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the authenticated user', () => {
|
||||||
|
authService.retrieveAuthenticatedUserByHref(EPersonMock._links.self.href).subscribe((user: EPerson) => {
|
||||||
expect(user).toBeDefined();
|
expect(user).toBeDefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -159,7 +172,7 @@ describe('AuthService test', () => {
|
|||||||
(state as any).core = Object.create({});
|
(state as any).core = Object.create({});
|
||||||
(state as any).core.auth = authenticatedState;
|
(state as any).core.auth = authenticatedState;
|
||||||
});
|
});
|
||||||
authService = new AuthService({}, window, undefined, authReqService, router, routeService, cookieService, store, linkService);
|
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should return true when user is logged in', () => {
|
it('should return true when user is logged in', () => {
|
||||||
@@ -221,7 +234,7 @@ describe('AuthService test', () => {
|
|||||||
(state as any).core = Object.create({});
|
(state as any).core = Object.create({});
|
||||||
(state as any).core.auth = authenticatedState;
|
(state as any).core.auth = authenticatedState;
|
||||||
});
|
});
|
||||||
authService = new AuthService({}, window, undefined, authReqService, router, routeService, cookieService, store, linkService);
|
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store);
|
||||||
storage = (authService as any).storage;
|
storage = (authService as any).storage;
|
||||||
routeServiceMock = TestBed.get(RouteService);
|
routeServiceMock = TestBed.get(RouteService);
|
||||||
routerStub = TestBed.get(Router);
|
routerStub = TestBed.get(Router);
|
||||||
@@ -250,7 +263,7 @@ describe('AuthService test', () => {
|
|||||||
expect(storage.remove).toHaveBeenCalled();
|
expect(storage.remove).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('should set redirect url to previous page', () => {
|
it('should set redirect url to previous page', () => {
|
||||||
spyOn(routeServiceMock, 'getHistory').and.callThrough();
|
spyOn(routeServiceMock, 'getHistory').and.callThrough();
|
||||||
spyOn(routerStub, 'navigateByUrl');
|
spyOn(routerStub, 'navigateByUrl');
|
||||||
authService.redirectAfterLoginSuccess(true);
|
authService.redirectAfterLoginSuccess(true);
|
||||||
@@ -258,7 +271,7 @@ describe('AuthService test', () => {
|
|||||||
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/collection/123');
|
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/collection/123');
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('should set redirect url to current page', () => {
|
it('should set redirect url to current page', () => {
|
||||||
spyOn(routeServiceMock, 'getHistory').and.callThrough();
|
spyOn(routeServiceMock, 'getHistory').and.callThrough();
|
||||||
spyOn(routerStub, 'navigateByUrl');
|
spyOn(routerStub, 'navigateByUrl');
|
||||||
authService.redirectAfterLoginSuccess(false);
|
authService.redirectAfterLoginSuccess(false);
|
||||||
@@ -266,7 +279,7 @@ describe('AuthService test', () => {
|
|||||||
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/home');
|
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/home');
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('should redirect to / and not to /login', () => {
|
it('should redirect to / and not to /login', () => {
|
||||||
spyOn(routeServiceMock, 'getHistory').and.returnValue(observableOf(['/login', '/login']));
|
spyOn(routeServiceMock, 'getHistory').and.returnValue(observableOf(['/login', '/login']));
|
||||||
spyOn(routerStub, 'navigateByUrl');
|
spyOn(routerStub, 'navigateByUrl');
|
||||||
authService.redirectAfterLoginSuccess(true);
|
authService.redirectAfterLoginSuccess(true);
|
||||||
@@ -274,7 +287,7 @@ describe('AuthService test', () => {
|
|||||||
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/');
|
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/');
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('should redirect to / when no redirect url is found', () => {
|
it('should redirect to / when no redirect url is found', () => {
|
||||||
spyOn(routeServiceMock, 'getHistory').and.returnValue(observableOf(['']));
|
spyOn(routeServiceMock, 'getHistory').and.returnValue(observableOf(['']));
|
||||||
spyOn(routerStub, 'navigateByUrl');
|
spyOn(routerStub, 'navigateByUrl');
|
||||||
authService.redirectAfterLoginSuccess(true);
|
authService.redirectAfterLoginSuccess(true);
|
||||||
|
@@ -4,12 +4,10 @@ import { HttpHeaders } from '@angular/common/http';
|
|||||||
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
|
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
|
||||||
|
|
||||||
import { Observable, of as observableOf } from 'rxjs';
|
import { Observable, of as observableOf } from 'rxjs';
|
||||||
import { distinctUntilChanged, filter, map, startWith, switchMap, take, withLatestFrom } from 'rxjs/operators';
|
import { distinctUntilChanged, filter, map, startWith, take, withLatestFrom } from 'rxjs/operators';
|
||||||
import { RouterReducerState } from '@ngrx/router-store';
|
import { RouterReducerState } from '@ngrx/router-store';
|
||||||
import { select, Store } from '@ngrx/store';
|
import { select, Store } from '@ngrx/store';
|
||||||
import { CookieAttributes } from 'js-cookie';
|
import { CookieAttributes } from 'js-cookie';
|
||||||
import { followLink } from '../../shared/utils/follow-link-config.model';
|
|
||||||
import { LinkService } from '../cache/builders/link.service';
|
|
||||||
|
|
||||||
import { EPerson } from '../eperson/models/eperson.model';
|
import { EPerson } from '../eperson/models/eperson.model';
|
||||||
import { AuthRequestService } from './auth-request.service';
|
import { AuthRequestService } from './auth-request.service';
|
||||||
@@ -24,6 +22,8 @@ import { ResetAuthenticationMessagesAction, SetRedirectUrlAction } from './auth.
|
|||||||
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
|
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
|
||||||
import { Base64EncodeUrl } from '../../shared/utils/encode-decode.util';
|
import { Base64EncodeUrl } from '../../shared/utils/encode-decode.util';
|
||||||
import { RouteService } from '../services/route.service';
|
import { RouteService } from '../services/route.service';
|
||||||
|
import { EPersonDataService } from '../eperson/eperson-data.service';
|
||||||
|
import { getFirstSucceededRemoteDataPayload } from '../shared/operators';
|
||||||
|
|
||||||
export const LOGIN_ROUTE = '/login';
|
export const LOGIN_ROUTE = '/login';
|
||||||
export const LOGOUT_ROUTE = '/logout';
|
export const LOGOUT_ROUTE = '/logout';
|
||||||
@@ -44,13 +44,13 @@ export class AuthService {
|
|||||||
|
|
||||||
constructor(@Inject(REQUEST) protected req: any,
|
constructor(@Inject(REQUEST) protected req: any,
|
||||||
@Inject(NativeWindowService) protected _window: NativeWindowRef,
|
@Inject(NativeWindowService) protected _window: NativeWindowRef,
|
||||||
protected authRequestService: AuthRequestService,
|
|
||||||
@Optional() @Inject(RESPONSE) private response: any,
|
@Optional() @Inject(RESPONSE) private response: any,
|
||||||
|
protected authRequestService: AuthRequestService,
|
||||||
|
protected epersonService: EPersonDataService,
|
||||||
protected router: Router,
|
protected router: Router,
|
||||||
protected routeService: RouteService,
|
protected routeService: RouteService,
|
||||||
protected storage: CookieService,
|
protected storage: CookieService,
|
||||||
protected store: Store<AppState>,
|
protected store: Store<AppState>
|
||||||
protected linkService: LinkService
|
|
||||||
) {
|
) {
|
||||||
this.store.pipe(
|
this.store.pipe(
|
||||||
select(isAuthenticated),
|
select(isAuthenticated),
|
||||||
@@ -123,10 +123,10 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the authenticated user
|
* Returns the href link to authenticated user
|
||||||
* @returns {User}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
public authenticatedUser(token: AuthTokenInfo): Observable<EPerson> {
|
public authenticatedUser(token: AuthTokenInfo): Observable<string> {
|
||||||
// Determine if the user has an existing auth session on the server
|
// Determine if the user has an existing auth session on the server
|
||||||
const options: HttpOptions = Object.create({});
|
const options: HttpOptions = Object.create({});
|
||||||
let headers = new HttpHeaders();
|
let headers = new HttpHeaders();
|
||||||
@@ -134,16 +134,25 @@ export class AuthService {
|
|||||||
headers = headers.append('Authorization', `Bearer ${token.accessToken}`);
|
headers = headers.append('Authorization', `Bearer ${token.accessToken}`);
|
||||||
options.headers = headers;
|
options.headers = headers;
|
||||||
return this.authRequestService.getRequest('status', options).pipe(
|
return this.authRequestService.getRequest('status', options).pipe(
|
||||||
map((status) => this.linkService.resolveLinks(status, followLink<AuthStatus>('eperson'))),
|
map((status: AuthStatus) => {
|
||||||
switchMap((status: AuthStatus) => {
|
|
||||||
if (status.authenticated) {
|
if (status.authenticated) {
|
||||||
return status.eperson.pipe(map((eperson) => eperson.payload));
|
return status._links.eperson.href;
|
||||||
} else {
|
} else {
|
||||||
throw(new Error('Not authenticated'));
|
throw(new Error('Not authenticated'));
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the authenticated user
|
||||||
|
* @returns {User}
|
||||||
|
*/
|
||||||
|
public retrieveAuthenticatedUserByHref(userHref: string): Observable<EPerson> {
|
||||||
|
return this.epersonService.findByHref(userHref).pipe(
|
||||||
|
getFirstSucceededRemoteDataPayload()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if token is present into browser storage and is valid. (NB Check is done only on SSR)
|
* Checks if token is present into browser storage and is valid. (NB Check is done only on SSR)
|
||||||
*/
|
*/
|
||||||
|
@@ -2,11 +2,9 @@ import { HttpHeaders } from '@angular/common/http';
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { filter, map, switchMap, take } from 'rxjs/operators';
|
import { filter, map, take } from 'rxjs/operators';
|
||||||
import { isNotEmpty } from '../../shared/empty.util';
|
import { isNotEmpty } from '../../shared/empty.util';
|
||||||
import { followLink } from '../../shared/utils/follow-link-config.model';
|
|
||||||
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
|
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
|
||||||
import { EPerson } from '../eperson/models/eperson.model';
|
|
||||||
import { CheckAuthenticationTokenAction } from './auth.actions';
|
import { CheckAuthenticationTokenAction } from './auth.actions';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { AuthStatus } from './models/auth-status.model';
|
import { AuthStatus } from './models/auth-status.model';
|
||||||
@@ -22,7 +20,7 @@ export class ServerAuthService extends AuthService {
|
|||||||
* Returns the authenticated user
|
* Returns the authenticated user
|
||||||
* @returns {User}
|
* @returns {User}
|
||||||
*/
|
*/
|
||||||
public authenticatedUser(token: AuthTokenInfo): Observable<EPerson> {
|
public authenticatedUser(token: AuthTokenInfo): Observable<string> {
|
||||||
// Determine if the user has an existing auth session on the server
|
// Determine if the user has an existing auth session on the server
|
||||||
const options: HttpOptions = Object.create({});
|
const options: HttpOptions = Object.create({});
|
||||||
let headers = new HttpHeaders();
|
let headers = new HttpHeaders();
|
||||||
@@ -35,10 +33,9 @@ export class ServerAuthService extends AuthService {
|
|||||||
|
|
||||||
options.headers = headers;
|
options.headers = headers;
|
||||||
return this.authRequestService.getRequest('status', options).pipe(
|
return this.authRequestService.getRequest('status', options).pipe(
|
||||||
map((status) => this.linkService.resolveLinks(status, followLink<AuthStatus>('eperson'))),
|
map((status: AuthStatus) => {
|
||||||
switchMap((status: AuthStatus) => {
|
|
||||||
if (status.authenticated) {
|
if (status.authenticated) {
|
||||||
return status.eperson.pipe(map((eperson) => eperson.payload));
|
return status._links.eperson.href;
|
||||||
} else {
|
} else {
|
||||||
throw(new Error('Not authenticated'));
|
throw(new Error('Not authenticated'));
|
||||||
}
|
}
|
||||||
|
@@ -5,8 +5,6 @@ import { AuthTokenInfo } from '../../core/auth/models/auth-token-info.model';
|
|||||||
import { EPerson } from '../../core/eperson/models/eperson.model';
|
import { EPerson } from '../../core/eperson/models/eperson.model';
|
||||||
import { isNotEmpty } from '../empty.util';
|
import { isNotEmpty } from '../empty.util';
|
||||||
import { EPersonMock } from './eperson-mock';
|
import { EPersonMock } from './eperson-mock';
|
||||||
import { RemoteData } from '../../core/data/remote-data';
|
|
||||||
import { createSuccessfulRemoteDataObject$ } from './utils';
|
|
||||||
|
|
||||||
export class AuthRequestServiceStub {
|
export class AuthRequestServiceStub {
|
||||||
protected mockUser: EPerson = EPersonMock;
|
protected mockUser: EPerson = EPersonMock;
|
||||||
@@ -28,7 +26,14 @@ export class AuthRequestServiceStub {
|
|||||||
if (this.validateToken(token)) {
|
if (this.validateToken(token)) {
|
||||||
authStatusStub.authenticated = true;
|
authStatusStub.authenticated = true;
|
||||||
authStatusStub.token = this.mockTokenInfo;
|
authStatusStub.token = this.mockTokenInfo;
|
||||||
authStatusStub.eperson = createSuccessfulRemoteDataObject$(this.mockUser);
|
authStatusStub._links = {
|
||||||
|
self: {
|
||||||
|
href: 'dspace.org/api/status',
|
||||||
|
},
|
||||||
|
eperson: {
|
||||||
|
href: this.mockUser._links.self.href
|
||||||
|
}
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
authStatusStub.authenticated = false;
|
authStatusStub.authenticated = false;
|
||||||
}
|
}
|
||||||
@@ -47,7 +52,14 @@ export class AuthRequestServiceStub {
|
|||||||
if (this.validateToken(token)) {
|
if (this.validateToken(token)) {
|
||||||
authStatusStub.authenticated = true;
|
authStatusStub.authenticated = true;
|
||||||
authStatusStub.token = this.mockTokenInfo;
|
authStatusStub.token = this.mockTokenInfo;
|
||||||
authStatusStub.eperson = createSuccessfulRemoteDataObject$(this.mockUser);
|
authStatusStub._links = {
|
||||||
|
self: {
|
||||||
|
href: 'dspace.org/api/status',
|
||||||
|
},
|
||||||
|
eperson: {
|
||||||
|
href: this.mockUser._links.self.href
|
||||||
|
}
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
authStatusStub.authenticated = false;
|
authStatusStub.authenticated = false;
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,6 @@ import { AuthStatus } from '../../core/auth/models/auth-status.model';
|
|||||||
import { AuthTokenInfo } from '../../core/auth/models/auth-token-info.model';
|
import { AuthTokenInfo } from '../../core/auth/models/auth-token-info.model';
|
||||||
import { EPersonMock } from './eperson-mock';
|
import { EPersonMock } from './eperson-mock';
|
||||||
import { EPerson } from '../../core/eperson/models/eperson.model';
|
import { EPerson } from '../../core/eperson/models/eperson.model';
|
||||||
import { RemoteData } from '../../core/data/remote-data';
|
|
||||||
import { createSuccessfulRemoteDataObject$ } from './utils';
|
import { createSuccessfulRemoteDataObject$ } from './utils';
|
||||||
|
|
||||||
export class AuthServiceStub {
|
export class AuthServiceStub {
|
||||||
@@ -30,14 +29,18 @@ export class AuthServiceStub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public authenticatedUser(token: AuthTokenInfo): Observable<EPerson> {
|
public authenticatedUser(token: AuthTokenInfo): Observable<string> {
|
||||||
if (token.accessToken === 'token_test') {
|
if (token.accessToken === 'token_test') {
|
||||||
return observableOf(EPersonMock);
|
return observableOf(EPersonMock._links.self.href);
|
||||||
} else {
|
} else {
|
||||||
throw(new Error('Message Error test'));
|
throw(new Error('Message Error test'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public retrieveAuthenticatedUserByHref(href: string): Observable<EPerson> {
|
||||||
|
return observableOf(EPersonMock);
|
||||||
|
}
|
||||||
|
|
||||||
public buildAuthHeader(token?: AuthTokenInfo): string {
|
public buildAuthHeader(token?: AuthTokenInfo): string {
|
||||||
return `Bearer ${token.accessToken}`;
|
return `Bearer ${token.accessToken}`;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user