I'm making an Angular 2 app with @ngrx/store and @ngrx/effects.
I'm struggling with understanding where to put logic outside of actions/effects etc and where to call service functions.
For example, with authentication...
- When a user clicks login this dispatches an
AUTH_REQUEST
action with the login credentials as the payload. - An effect looks for this action an calls the API.
- A successful result calls the
AUTH_SUCCESS
action with the token, username etc in the response object as a payload which goes to the reducer to update theAuthState
.
eg: In AuthEffects
@Effect() authenticate$ = this.updates$
.whenAction(AuthActions.AUTHENTICATE_REQUEST)
.switchMap(update => this.api.post('/authenticate', update.action.payload)
.map((res:any) => ({type: AuthActions.AUTHENTICATE_SUCCESS, payload: res.json()}))
.catch((err:any) => Observable.of({ type: AuthActions.AUTHENTICATE_ERROR, payload: err }))
);
In AuthReducer
case AuthActions.AUTHENTICATE_SUCCESS:
return Object.assign({}, state, <AuthState>{
processing: false,
failed: false,
isLoggedIn: true,
token: action.payload.token,
username: action.payload.username,
accountId: action.payload.accountId,
});
What I want to know is:
- Where to call the router to change pages after an
AUTH_SUCCESS
action is processed. Do I do this from within the effects Reactive chain or....?? - I have a
AuthService
that needs to store the credentials (token etc) in LocalStorage. Where should I call this to "store the token" ieauthService.store(userCredentials)
.
Any help appreciated.
CodeSequence/ngrx-store-router
? – cartant