import { routes } from './app.routes';
import { provideStore } from '@ngrx/store';
+import { provideEffects } from '@ngrx/effects';
+import { loginReducers } from './store/login.reducers';
+import { LoginEffects } from './store/login.effects';
export const appConfig: ApplicationConfig = {
providers: [
- provideStore({}),
+ provideStore({ login : loginReducers }),
+ provideEffects([LoginEffects]),
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes)
]
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
+import { LoginComponent } from './login/login.component';
export const routes: Routes = [
{ path: '', component: HomeComponent }, // Default-Route
+ { path: 'login', component: LoginComponent } // Login-Route
];
import { Component, inject } from '@angular/core';
+import { NgIf } from '@angular/common';
+import { FormsModule } from "@angular/forms";
import { Store } from '@ngrx/store';
import { login } from '../store/login.actions';
import { selectError, selectIsLoading, selectToken } from '../store/login.selectors';
@Component({
selector: 'app-login',
- imports: [],
+ imports: [
+ NgIf,
+ FormsModule,
+ ],
templateUrl: './login.component.html',
styleUrl: './login.component.less'
})
export class LoginComponent {
private store = inject(Store);
- username = '';
- password = '';
- token = '';
- error = '';
- isLoading = false;
+ username : string|null = null;
+ password : string|null = null;
+ token : string|null = null;
+ error : string|null = null;
+ isLoading : boolean = false;
ngOnInit() {
this.store.select(selectToken).subscribe(token => (this.token = token));
--- /dev/null
+import { TestBed } from '@angular/core/testing';
+
+import { AuthService } from './auth.service';
+
+describe('AuthService', () => {
+ let service: AuthService;
+
+ beforeEach(() => {
+ TestBed.configureTestingModule({});
+ service = TestBed.inject(AuthService);
+ });
+
+ it('should be created', () => {
+ expect(service).toBeTruthy();
+ });
+});
--- /dev/null
+import { Injectable } from '@angular/core';
+import { Observable, of } from 'rxjs';
+
+@Injectable({
+ providedIn: 'root'
+})
+export class AuthService {
+ private username : string|undefined;
+
+ login(username : string, password : string) : Observable<string> {
+ return of(username);
+ }
+}
export const login = createAction(
'[Login] User Login',
- props<{ username: string, password: string }>()
+ props<{ username: string|null, password: string|null }>()
);
export const loginSuccess = createAction(
-import { Injectable } from '@angular/core';
+import { inject, Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
-import { map, catchError, switchMap } from 'rxjs/operators';
+import { catchError, map, switchMap } from 'rxjs/operators';
import { of } from 'rxjs';
-import { loginSuccess, loginFailure } from './login.actions';
+import { loginFailure, loginSuccess } from './login.actions';
import { AuthService } from '../services/auth.service';
@Injectable()
export class LoginEffects {
+ private actions$ = inject(Actions);
+ private authService = inject(AuthService);
+
login$ = createEffect(() =>
this.actions$.pipe(
ofType('[Login] User Login'),
)
)
);
-
- constructor(private actions$: Actions, private authService: AuthService) {}
}
import { createReducer, on } from '@ngrx/store';
-import { login, loginSuccess, loginFailure } from './login.actions';
+import { login, loginFailure, loginSuccess } from './login.actions';
export interface State {
- token: string;
- error: string;
+ token: string|null;
+ error: string|null;
isLoading: boolean;
}