mirror of
https://github.com/a-mayb3/KanbanCloneAngular.git
synced 2026-03-21 18:05:38 +01:00
minor fixes
This commit is contained in:
parent
d1e016b7df
commit
d31630db18
5 changed files with 157 additions and 94 deletions
|
|
@ -3,23 +3,45 @@ import { FormsModule } from '@angular/forms';
|
|||
import { Router } from '@angular/router';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { AuthService } from '../../services/auth.service';
|
||||
import { catchError, of, throwError } from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
standalone: true,
|
||||
imports: [FormsModule, CommonModule],
|
||||
templateUrl: './login.component.html',
|
||||
styleUrl: './login.component.css'
|
||||
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(['/']);
|
||||
return;
|
||||
}
|
||||
|
||||
this.authService
|
||||
.checkSession()
|
||||
.pipe(
|
||||
catchError((error) => {
|
||||
if (error?.status === 401 || error?.status === 422) {
|
||||
return of(null);
|
||||
}
|
||||
return throwError(() => error);
|
||||
}),
|
||||
)
|
||||
.subscribe({
|
||||
next: (user) => {
|
||||
if (user) {
|
||||
this.router.navigate(['/']);
|
||||
}
|
||||
},
|
||||
error: (error) => {
|
||||
this.errorMessage.set(error?.error?.message || 'Session check failed.');
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
email = '';
|
||||
|
|
@ -36,25 +58,27 @@ export class LoginComponent implements OnInit {
|
|||
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.'
|
||||
);
|
||||
}
|
||||
});
|
||||
this.authService
|
||||
.login({
|
||||
email: this.email,
|
||||
password: this.password,
|
||||
})
|
||||
.subscribe({
|
||||
next: (response) => {
|
||||
this.isLoading.set(false);
|
||||
if (response.success || response.user) {
|
||||
// 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