Adopted the implementation of the article for the component
authorKai Moritz <kai@juplo.de>
Sun, 29 Jun 2025 13:53:01 +0000 (15:53 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 6 Sep 2025 10:32:49 +0000 (12:32 +0200)
link: https://medium.com/@tranan.aptech/building-a-secure-login-system-with-angular-and-ngrx-a-step-by-step-guide-b1987cdd5626[article
src/app/login/login.component.ts

index b5029f2..30c71ee 100644 (file)
@@ -1,11 +1,41 @@
-import { Component } from '@angular/core';
+import { Store } from '@ngrx/store';
+import { login } from './store/login.actions';
+import { selectToken, selectError, selectIsLoading } from './store/login.selectors';
 
 @Component({
   selector: 'app-login',
-  imports: [],
-  templateUrl: './login.component.html',
-  styleUrl: './login.component.less'
+  template: `
+    <form (ngSubmit)="onSubmit()">
+      <label>
+        Username:
+        <input type="text" [(ngModel)]="username" name="username" required>
+      </label>
+      <br>
+      <label>
+        Password:
+        <input type="password" [(ngModel)]="password" name="password" required>
+      </label>
+      <br>
+      <button type="submit" [disabled]="isLoading">Login</button>
+    </form>
+    <div *ngIf="error">{{ error }}</div>
+    <div *ngIf="token">Welcome, {{ username }}! Your token is {{ token }}</div>
+  `
 })
 export class LoginComponent {
+  username = '';
+  password = '';
+  token = '';
+  error = '';
+  isLoading = false;
 
+  constructor(private store: Store) {
+    this.store.select(selectToken).subscribe(token => (this.token = token));
+    this.store.select(selectError).subscribe(error => (this.error = error));
+    this.store.select(selectIsLoading).subscribe(isLoading => (this.isLoading = isLoading));
+  }
+
+  onSubmit() {
+    this.store.dispatch(login({ username: this.username, password: this.password }));
+  }
 }