Added refresh token functionality

This commit is contained in:
Giuseppe Digilio
2018-02-26 15:41:07 +01:00
parent 2637f1c28e
commit 696b8b73f5
22 changed files with 301 additions and 114 deletions

View File

@@ -18,7 +18,7 @@ import {
AuthenticationErrorAction,
AuthenticationSuccessAction, CheckAuthenticationTokenErrorAction,
LogOutErrorAction,
LogOutSuccessAction, RegistrationAction,
LogOutSuccessAction, RefreshTokenAction, RefreshTokenErrorAction, RefreshTokenSuccessAction, RegistrationAction,
RegistrationErrorAction,
RegistrationSuccessAction
} from './auth.actions';
@@ -41,11 +41,11 @@ export class AuthEffects {
.ofType(AuthActionTypes.AUTHENTICATE)
.switchMap((action: AuthenticateAction) => {
return this.authService.authenticate(action.payload.email, action.payload.password)
.first()
.map((response: AuthStatus) => new AuthenticationSuccessAction(response.token))
.catch((error) => Observable.of(new AuthenticationErrorAction(error)));
});
// It means "reacts to this action but don't send another"
@Effect()
public authenticateSuccess: Observable<Action> = this.actions$
.ofType(AuthActionTypes.AUTHENTICATE_SUCCESS)
@@ -61,6 +61,7 @@ export class AuthEffects {
.catch((error) => Observable.of(new AuthenticatedErrorAction(error)));
});
// It means "reacts to this action but don't send another"
@Effect({dispatch: false})
public authenticatedError: Observable<Action> = this.actions$
.ofType(AuthActionTypes.AUTHENTICATED_ERROR)
@@ -85,6 +86,21 @@ export class AuthEffects {
.catch((error) => Observable.of(new RegistrationErrorAction(error)));
});
@Effect()
public refreshToken: Observable<Action> = this.actions$
.ofType(AuthActionTypes.REFRESH_TOKEN)
.switchMap((action: RefreshTokenAction) => {
return this.authService.refreshAuthenticationToken(action.payload)
.map((token: AuthTokenInfo) => new RefreshTokenSuccessAction(token))
.catch((error) => Observable.of(new RefreshTokenErrorAction()));
});
// It means "reacts to this action but don't send another"
@Effect({dispatch: false})
public refreshTokenSuccess: Observable<Action> = this.actions$
.ofType(AuthActionTypes.REFRESH_TOKEN_SUCCESS)
.do((action: RefreshTokenSuccessAction) => this.authService.replaceToken(action.payload));
/**
* When the store is rehydrated in the browser,
* clear a possible invalid token or authentication errors