import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Store } from '@ngrx/store';
-import { selectToken } from '../store/login.selectors';
+import { selectUsername } from '../store/login.selectors';
@Component({
selector: 'app-home',
username$;
constructor(store : Store) {
- this.username$ = store.select(selectToken);
+ this.username$ = store.select(selectUsername);
}
}
import { Observable, of, throwError } from 'rxjs';
import { delay } from 'rxjs/operators';
import { Store } from '@ngrx/store';
-import { selectError, selectToken } from '../store/login.selectors';
+import { selectError, selectUsername } from '../store/login.selectors';
@Injectable({
providedIn: 'root'
private username : string|undefined;
constructor() {
- this.store.select(selectToken).subscribe(username => {
+ this.store.select(selectUsername).subscribe(username => {
this.username = username;
this.router.navigate(['']);
});
export const loginSuccess = createAction(
'[Login] Login Success',
- props<{ token: string }>()
+ props<{ username: string }>()
);
export const loginFailure = createAction(
ofType('[Login] User Login'),
switchMap(({ username, password }) =>
this.authService.login(username, password).pipe(
- map(token => loginSuccess({ token })),
+ map(username => loginSuccess({ username })),
catchError(error => of(loginFailure({ error })))
)
)
import { login, loginFailure, loginSuccess } from './login.actions';
export interface State {
- token: string|undefined;
+ username: string|undefined;
error: string|undefined;
isLoading: boolean;
}
const initialState: State = {
- token: undefined,
+ username: undefined,
error: undefined,
isLoading: false
};
export const loginReducers = createReducer(initialState,
on(login, state => ({ ...state, isLoading: true })),
- on(loginSuccess, (state, { token }) => ({ ...state, token, error: undefined, isLoading: false })),
- on(loginFailure, (state, { error }) => ({ ...state, token: undefined, error, isLoading: false }))
+ on(loginSuccess, (state, { username }) => ({ ...state, username, error: undefined, isLoading: false })),
+ on(loginFailure, (state, { error }) => ({ ...state, username: undefined, error, isLoading: false }))
);
const selectLogin = (state: any) => state.login;
-export const selectToken = createSelector(
+export const selectUsername = createSelector(
selectLogin,
- (state) => state.token
+ (state) => state.username
);
export const selectError = createSelector(