mirror of
https://github.com/a-mayb3/KanbanCloneAngular.git
synced 2026-03-21 18:05:38 +01:00
Base login page
This commit is contained in:
parent
0d82ea47c3
commit
1d39dffd56
3 changed files with 212 additions and 0 deletions
60
src/app/pages/login/login.component.ts
Normal file
60
src/app/pages/login/login.component.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { Component, inject, signal, OnInit } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { Router } from '@angular/router';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { AuthService } from '../../services/auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
standalone: true,
|
||||
imports: [FormsModule, CommonModule],
|
||||
templateUrl: './login.component.html',
|
||||
styleUrl: './login.component.css'
|
||||
})
|
||||
export class LoginComponent implements OnInit {
|
||||
private authService = inject(AuthService);
|
||||
private router = inject(Router);
|
||||
|
||||
ngOnInit() {
|
||||
// If already logged in, redirect to home
|
||||
if (this.authService.isAuthenticated()) {
|
||||
this.router.navigate(['/']);
|
||||
}
|
||||
}
|
||||
|
||||
email = '';
|
||||
password = '';
|
||||
isLoading = signal(false);
|
||||
errorMessage = signal('');
|
||||
|
||||
onSubmit() {
|
||||
if (!this.email || !this.password) {
|
||||
this.errorMessage.set('Please fill in all fields');
|
||||
return;
|
||||
}
|
||||
|
||||
this.isLoading.set(true);
|
||||
this.errorMessage.set('');
|
||||
|
||||
this.authService.login({
|
||||
email: this.email,
|
||||
password: this.password
|
||||
}).subscribe({
|
||||
next: (response) => {
|
||||
this.isLoading.set(false);
|
||||
if (response.success) {
|
||||
// Redirect to home/dashboard after successful login
|
||||
this.router.navigate(['/']);
|
||||
} else {
|
||||
this.errorMessage.set(response.message || 'Login failed');
|
||||
}
|
||||
},
|
||||
error: (error) => {
|
||||
this.isLoading.set(false);
|
||||
this.errorMessage.set(
|
||||
error.error?.message || 'Login failed. Please check your credentials.'
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue