mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 10:34:15 +00:00
Improvement for authentication module
This commit is contained in:
@@ -23,23 +23,7 @@ import {
|
||||
RegistrationSuccessAction
|
||||
} from './auth.actions';
|
||||
import { Eperson } from '../eperson/models/eperson.model';
|
||||
|
||||
/**
|
||||
* Effects offer a way to isolate and easily test side-effects within your
|
||||
* application.
|
||||
* The `toPayload` helper function returns just
|
||||
* the payload of the currently dispatched action, useful in
|
||||
* instances where the current state is not necessary.
|
||||
*
|
||||
* Documentation on `toPayload` can be found here:
|
||||
* https://github.com/ngrx/effects/blob/master/docs/api.md#topayload
|
||||
*
|
||||
* If you are unfamiliar with the operators being used in these examples, please
|
||||
* check out the sources below:
|
||||
*
|
||||
* Official Docs: http://reactivex.io/rxjs/manual/overview.html#categories-of-operators
|
||||
* RxJS 5 Operators By Example: https://gist.github.com/btroncone/d6cf141d6f2c00dc6b35
|
||||
*/
|
||||
import { AuthStatus } from './models/auth-status.model';
|
||||
|
||||
@Injectable()
|
||||
export class AuthEffects {
|
||||
@@ -51,18 +35,29 @@ export class AuthEffects {
|
||||
@Effect()
|
||||
public authenticate: Observable<Action> = this.actions$
|
||||
.ofType(AuthActionTypes.AUTHENTICATE)
|
||||
.debounceTime(500)
|
||||
.switchMap((action: AuthenticateAction) => {
|
||||
return this.authService.authenticate(action.payload.email, action.payload.password)
|
||||
.map((user: Eperson) => new AuthenticationSuccessAction(user))
|
||||
.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)
|
||||
.do((action: AuthenticationSuccessAction) => this.authService.storeToken(action.payload))
|
||||
.map((action: AuthenticationSuccessAction) => new AuthenticatedAction(action.payload))
|
||||
|
||||
@Effect({dispatch: false})
|
||||
public logOutSuccess: Observable<Action> = this.actions$
|
||||
.ofType(AuthActionTypes.LOG_OUT_SUCCESS)
|
||||
.do((action: LogOutSuccessAction) => this.authService.removeToken());
|
||||
|
||||
@Effect()
|
||||
public authenticated: Observable<Action> = this.actions$
|
||||
.ofType(AuthActionTypes.AUTHENTICATED)
|
||||
.switchMap((action: AuthenticatedAction) => {
|
||||
return this.authService.authenticatedUser()
|
||||
return this.authService.authenticatedUser(action.payload)
|
||||
.map((user: Eperson) => new AuthenticatedSuccessAction((user !== null), user))
|
||||
.catch((error) => Observable.of(new AuthenticatedErrorAction(error)));
|
||||
});
|
||||
@@ -70,7 +65,7 @@ export class AuthEffects {
|
||||
@Effect()
|
||||
public createUser: Observable<Action> = this.actions$
|
||||
.ofType(AuthActionTypes.REGISTRATION)
|
||||
.debounceTime(500)
|
||||
.debounceTime(500) // to remove when functionality is implemented
|
||||
.switchMap((action: RegistrationAction) => {
|
||||
return this.authService.create(action.payload)
|
||||
.map((user: Eperson) => new RegistrationSuccessAction(user))
|
||||
|
Reference in New Issue
Block a user