mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 10:34:15 +00:00
Added auth token to auth state
This commit is contained in:
@@ -75,11 +75,12 @@ export class AuthenticatedSuccessAction implements Action {
|
|||||||
public type: string = AuthActionTypes.AUTHENTICATED_SUCCESS;
|
public type: string = AuthActionTypes.AUTHENTICATED_SUCCESS;
|
||||||
payload: {
|
payload: {
|
||||||
authenticated: boolean;
|
authenticated: boolean;
|
||||||
|
authToken: AuthTokenInfo;
|
||||||
user: Eperson
|
user: Eperson
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(authenticated: boolean, user: Eperson) {
|
constructor(authenticated: boolean, authToken: AuthTokenInfo, user: Eperson) {
|
||||||
this.payload = { authenticated, user };
|
this.payload = { authenticated, authToken, user };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -57,7 +57,7 @@ export class AuthEffects {
|
|||||||
.ofType(AuthActionTypes.AUTHENTICATED)
|
.ofType(AuthActionTypes.AUTHENTICATED)
|
||||||
.switchMap((action: AuthenticatedAction) => {
|
.switchMap((action: AuthenticatedAction) => {
|
||||||
return this.authService.authenticatedUser(action.payload)
|
return this.authService.authenticatedUser(action.payload)
|
||||||
.map((user: Eperson) => new AuthenticatedSuccessAction((user !== null), user))
|
.map((user: Eperson) => new AuthenticatedSuccessAction((user !== null), action.payload, user))
|
||||||
.catch((error) => Observable.of(new AuthenticatedErrorAction(error)));
|
.catch((error) => Observable.of(new AuthenticatedErrorAction(error)));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -3,11 +3,12 @@ import {
|
|||||||
AddAuthenticationMessageAction,
|
AddAuthenticationMessageAction,
|
||||||
AuthActions, AuthActionTypes, AuthenticatedSuccessAction, AuthenticationErrorAction,
|
AuthActions, AuthActionTypes, AuthenticatedSuccessAction, AuthenticationErrorAction,
|
||||||
AuthenticationSuccessAction, LogOutErrorAction, RedirectWhenAuthenticationIsRequiredAction,
|
AuthenticationSuccessAction, LogOutErrorAction, RedirectWhenAuthenticationIsRequiredAction,
|
||||||
RedirectWhenTokenExpiredAction, SetRedirectUrlAction
|
RedirectWhenTokenExpiredAction, RefreshTokenSuccessAction, SetRedirectUrlAction
|
||||||
} from './auth.actions';
|
} from './auth.actions';
|
||||||
|
|
||||||
// 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';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The auth state.
|
* The auth state.
|
||||||
@@ -18,6 +19,9 @@ export interface AuthState {
|
|||||||
// boolean if user is authenticated
|
// boolean if user is authenticated
|
||||||
authenticated: boolean;
|
authenticated: boolean;
|
||||||
|
|
||||||
|
// the authentication token
|
||||||
|
authToken?: AuthTokenInfo;
|
||||||
|
|
||||||
// error message
|
// error message
|
||||||
error?: string;
|
error?: string;
|
||||||
|
|
||||||
@@ -68,6 +72,7 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
case AuthActionTypes.AUTHENTICATED_ERROR:
|
case AuthActionTypes.AUTHENTICATED_ERROR:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
|
authToken: undefined,
|
||||||
error: (action as AuthenticationErrorAction).payload.message,
|
error: (action as AuthenticationErrorAction).payload.message,
|
||||||
loaded: true,
|
loaded: true,
|
||||||
loading: false
|
loading: false
|
||||||
@@ -76,6 +81,7 @@ 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,
|
||||||
loaded: true,
|
loaded: true,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
loading: false,
|
loading: false,
|
||||||
@@ -87,6 +93,7 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
case AuthActionTypes.REGISTRATION_ERROR:
|
case AuthActionTypes.REGISTRATION_ERROR:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
|
authToken: undefined,
|
||||||
error: (action as AuthenticationErrorAction).payload.message,
|
error: (action as AuthenticationErrorAction).payload.message,
|
||||||
loading: false
|
loading: false
|
||||||
});
|
});
|
||||||
@@ -115,6 +122,7 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
case AuthActionTypes.REFRESH_TOKEN_ERROR:
|
case AuthActionTypes.REFRESH_TOKEN_ERROR:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
|
authToken: undefined,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
loaded: false,
|
loaded: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
@@ -127,6 +135,7 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
case AuthActionTypes.REDIRECT_TOKEN_EXPIRED:
|
case AuthActionTypes.REDIRECT_TOKEN_EXPIRED:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
|
authToken: undefined,
|
||||||
loaded: false,
|
loaded: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
info: (action as RedirectWhenTokenExpiredAction as RedirectWhenAuthenticationIsRequiredAction).payload,
|
info: (action as RedirectWhenTokenExpiredAction as RedirectWhenAuthenticationIsRequiredAction).payload,
|
||||||
@@ -136,6 +145,7 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
case AuthActionTypes.REGISTRATION:
|
case AuthActionTypes.REGISTRATION:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
|
authToken: undefined,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
loading: true,
|
loading: true,
|
||||||
info: undefined
|
info: undefined
|
||||||
@@ -151,6 +161,7 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
|
|
||||||
case AuthActionTypes.REFRESH_TOKEN_SUCCESS:
|
case AuthActionTypes.REFRESH_TOKEN_SUCCESS:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
|
authToken: (action as RefreshTokenSuccessAction).payload,
|
||||||
refreshing: false,
|
refreshing: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -161,10 +172,7 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
|
|
||||||
case AuthActionTypes.RESET_MESSAGES:
|
case AuthActionTypes.RESET_MESSAGES:
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
authenticated: state.authenticated,
|
|
||||||
error: undefined,
|
error: undefined,
|
||||||
loaded: state.loaded,
|
|
||||||
loading: state.loading,
|
|
||||||
info: undefined,
|
info: undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -234,6 +242,14 @@ export const _isLoading = (state: AuthState) => state.loading;
|
|||||||
*/
|
*/
|
||||||
export const _isRefreshing = (state: AuthState) => state.refreshing;
|
export const _isRefreshing = (state: AuthState) => state.refreshing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the authentication token.
|
||||||
|
* @function _getAuthenticationToken
|
||||||
|
* @param {State} state
|
||||||
|
* @returns {AuthToken}
|
||||||
|
*/
|
||||||
|
export const _getAuthenticationToken = (state: AuthState) => state.authToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the sign out error.
|
* Returns the sign out error.
|
||||||
* @function _getLogOutError
|
* @function _getLogOutError
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import { Inject, Injectable } from '@angular/core';
|
import { Inject, Injectable } from '@angular/core';
|
||||||
import { NavigationExtras, PRIMARY_OUTLET, Router, UrlSegmentGroup, UrlTree } from '@angular/router';
|
import { PRIMARY_OUTLET, Router, UrlSegmentGroup, UrlTree } from '@angular/router';
|
||||||
|
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
import { map, withLatestFrom } from 'rxjs/operators';
|
import { map, withLatestFrom } from 'rxjs/operators';
|
||||||
@@ -12,7 +12,7 @@ import { AuthStatus } from './models/auth-status.model';
|
|||||||
import { AuthTokenInfo, TOKENITEM } from './models/auth-token-info.model';
|
import { AuthTokenInfo, TOKENITEM } from './models/auth-token-info.model';
|
||||||
import { isEmpty, isNotEmpty, isNotNull, isNotUndefined } from '../../shared/empty.util';
|
import { isEmpty, isNotEmpty, isNotNull, isNotUndefined } from '../../shared/empty.util';
|
||||||
import { CookieService } from '../../shared/services/cookie.service';
|
import { CookieService } from '../../shared/services/cookie.service';
|
||||||
import { getRedirectUrl, isAuthenticated, isTokenRefreshing } from './selectors';
|
import { getAuthenticationToken, getRedirectUrl, isAuthenticated, isTokenRefreshing } from './selectors';
|
||||||
import { AppState, routerStateSelector } from '../../app.reducer';
|
import { AppState, routerStateSelector } from '../../app.reducer';
|
||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
import { ResetAuthenticationMessagesAction, SetRedirectUrlAction } from './auth.actions';
|
import { ResetAuthenticationMessagesAction, SetRedirectUrlAction } from './auth.actions';
|
||||||
@@ -212,13 +212,16 @@ export class AuthService {
|
|||||||
* @returns {AuthTokenInfo}
|
* @returns {AuthTokenInfo}
|
||||||
*/
|
*/
|
||||||
public getToken(): AuthTokenInfo {
|
public getToken(): AuthTokenInfo {
|
||||||
// Retrieve authentication token info and check if is valid
|
let token: AuthTokenInfo;
|
||||||
const token = this.storage.get(TOKENITEM);
|
this.store.select(getAuthenticationToken)
|
||||||
if (isNotEmpty(token) && token.hasOwnProperty('accessToken') && isNotEmpty(token.accessToken)) {
|
.subscribe((authTokenInfo: AuthTokenInfo) => {
|
||||||
return token;
|
// Retrieve authentication token info and check if is valid
|
||||||
} else {
|
token = isNotEmpty(authTokenInfo) ? authTokenInfo : this.storage.get(TOKENITEM);
|
||||||
return null;
|
if (isEmpty(token) || !token.hasOwnProperty('accessToken') || isEmpty(token.accessToken)) {
|
||||||
}
|
token = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -83,6 +83,14 @@ export const isAuthenticationLoading = createSelector(getAuthState, auth._isLoad
|
|||||||
*/
|
*/
|
||||||
export const isTokenRefreshing = createSelector(getAuthState, auth._isRefreshing);
|
export const isTokenRefreshing = createSelector(getAuthState, auth._isRefreshing);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the authentication token.
|
||||||
|
* @function getAuthenticationToken
|
||||||
|
* @param {State} state
|
||||||
|
* @returns {AuthToken}
|
||||||
|
*/
|
||||||
|
export const getAuthenticationToken = createSelector(getAuthState, auth._getAuthenticationToken);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the log out error.
|
* Returns the log out error.
|
||||||
* @function getLogOutError
|
* @function getLogOutError
|
||||||
|
Reference in New Issue
Block a user