From: Kai Moritz Date: Mon, 15 Sep 2025 21:08:30 +0000 (+0200) Subject: refactor: renamed `token` to `username` X-Git-Url: https://juplo.de/gitweb/?a=commitdiff_plain;h=3cddf7d20475f2862c67e8f03395846cdf5f663d;p=demos%2Fangular%2Fstored-login refactor: renamed `token` to `username` --- diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index b6a6b41..410b82c 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -1,7 +1,7 @@ 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', @@ -13,6 +13,6 @@ export class HomeComponent { username$; constructor(store : Store) { - this.username$ = store.select(selectToken); + this.username$ = store.select(selectUsername); } } diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 31885f6..26301db 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -3,7 +3,7 @@ import { Router } from "@angular/router"; 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' @@ -15,7 +15,7 @@ export class AuthService { private username : string|undefined; constructor() { - this.store.select(selectToken).subscribe(username => { + this.store.select(selectUsername).subscribe(username => { this.username = username; this.router.navigate(['']); }); diff --git a/src/app/store/login.actions.ts b/src/app/store/login.actions.ts index 1319408..2e24ec3 100644 --- a/src/app/store/login.actions.ts +++ b/src/app/store/login.actions.ts @@ -7,7 +7,7 @@ export const login = createAction( export const loginSuccess = createAction( '[Login] Login Success', - props<{ token: string }>() + props<{ username: string }>() ); export const loginFailure = createAction( diff --git a/src/app/store/login.effects.ts b/src/app/store/login.effects.ts index b0ba0c8..7294a23 100644 --- a/src/app/store/login.effects.ts +++ b/src/app/store/login.effects.ts @@ -15,7 +15,7 @@ export class LoginEffects { 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 }))) ) ) diff --git a/src/app/store/login.reducers.ts b/src/app/store/login.reducers.ts index 98ce5fc..e3043aa 100644 --- a/src/app/store/login.reducers.ts +++ b/src/app/store/login.reducers.ts @@ -2,19 +2,19 @@ import { createReducer, on } from '@ngrx/store'; 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 })) ); diff --git a/src/app/store/login.selectors.ts b/src/app/store/login.selectors.ts index 1ebe7e3..5451095 100644 --- a/src/app/store/login.selectors.ts +++ b/src/app/store/login.selectors.ts @@ -2,9 +2,9 @@ import { createSelector } from '@ngrx/store'; 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(