benoit-bremaud / angular-social-network

Angular is better than Somfony
MIT License
1 stars 0 forks source link

Implement user login form #7

Closed benoit-bremaud closed 3 months ago

benoit-bremaud commented 3 months ago

Description : Create a login form for existing users.

Steps :

benoit-bremaud commented 3 months ago
benoit-bremaud commented 3 months ago
benoit-bremaud commented 3 months ago

Implement the Login Form in the New Component

@Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'], standalone: true, imports: [CommonModule, FormsModule] }) export class LoginComponent { email: string = ''; password: string = '';

constructor(private authService: AuthService) {}

login(): void { const credentials = { email: this.email, password: this.password }; this.authService.login(credentials).subscribe( response => { console.log('Login successful', response); // Handle successful login, e.g., redirect to dashboard }, error => { console.error('Login failed', error); } ); } }

benoit-bremaud commented 3 months ago

Implement the Login Form in the New Component

  • [x] Update src/app/components/login/login.component.html
<div class="login-form">
  <h2>Login</h2>
  <form (ngSubmit)="login()">
    <div class="form-group">
      <label for="email">Email</label>
      <input type="email" id="email" [(ngModel)]="email" name="email" required />
    </div>
    <div class="form-group">
      <label for="password">Password</label>
      <input type="password" id="password" [(ngModel)]="password" name="password" required />
    </div>
    <button type="submit">Login</button>
  </form>
</div>
  • [x] Update src/app/components/login/login.component.ts
import { Component } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  standalone: true,
  imports: [CommonModule, FormsModule]
})
export class LoginComponent {
  email: string = '';
  password: string = '';

  constructor(private authService: AuthService) {}

  login(): void {
    const credentials = { email: this.email, password: this.password };
    this.authService.login(credentials).subscribe(
      response => {
        console.log('Login successful', response);
        // Handle successful login, e.g., redirect to dashboard
      },
      error => {
        console.error('Login failed', error);
      }
    );
  }
}

login.composent.ts modifications :

import { AuthService } from '../../services/auth.service';
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-login',
  standalone: true,
  imports: [CommonModule, FormsModule],
  templateUrl: './login.component.html',
  styleUrl: './login.component.scss'
})
export class LoginComponent {
  email: string = '';
  password: string = '';

  constructor(private authService: AuthService) {}

  login(): void {
    const credentials = { email: this.email, password: this.password };
    this.authService.login(credentials).subscribe({
      next: (response: any) => {
        console.log('Login successful', response);
      },
      error: (error: any) => {
        console.error('Login failed', error);
      },
      complete: () => {
        console.log('Login completed');
      }
    });
  }
}
benoit-bremaud commented 3 months ago

export const routes: Routes = [ { path: 'register', component: RegisterComponent }, { path: 'login', component: LoginComponent }, // Other routes ];

benoit-bremaud commented 3 months ago

@Component({ selector: 'app-root', template: <router-outlet></router-outlet>, standalone: true, imports: [RouterModule] }) export class AppComponent {}

benoit-bremaud commented 3 months ago
  • [x] Update src/app/app.component.ts
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';

@Component({
  selector: 'app-root',
  template: `<router-outlet></router-outlet>`,
  standalone: true,
  imports: [RouterModule]
})
export class AppComponent {}

The main differences between the two snippets are the usage of inline template vs templateUrl and the imports array.

First Snippet

import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [RouterOutlet],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'frontend';
}

Second Snippet

import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';

@Component({
  selector: 'app-root',
  template: `<router-outlet></router-outlet>`,
  standalone: true,
  imports: [RouterModule]
})
export class AppComponent {}

Differences Explained

  1. Template vs. TemplateUrl:

    • First Snippet: Uses templateUrl which points to an external HTML file (./app.component.html). This is generally used when the template is larger and more complex.
    • Second Snippet: Uses template which directly includes the HTML content (<router-outlet></router-outlet>) inline. This is often used for very simple templates.
  2. StyleUrl vs. No Styles:

    • First Snippet: Includes styleUrls which points to an external stylesheet (./app.component.scss). This allows you to separate your styles into an external file.
    • Second Snippet: Does not include any styles. You can add styles if needed using styles or styleUrls properties.
  3. Imports Array:

    • First Snippet: Imports RouterOutlet from @angular/router. This allows you to use the <router-outlet></router-outlet> directive in the external template file.
    • Second Snippet: Imports RouterModule from @angular/router. This provides routing functionalities, though it might be a bit broader than necessary if only using <router-outlet>.

Usage Context

Which One to Use?

Updated Code Snippet for Consistency

To keep the consistency with your previous setup where the template and styles are external, you should use the first snippet structure. Here's the updated version with the necessary adjustments:

import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [RouterOutlet],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'frontend';
}

This approach allows for better organization and is more scalable for larger applications.

benoit-bremaud commented 3 months ago

Test the Login Form Functionality