80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||
import { Router } from '@angular/router';
|
||
import { MatSnackBar } from '@angular/material/snack-bar';
|
||
import { AuthService } from '../../services/auth.service';
|
||
import { CommonModule } from '@angular/common';
|
||
import { ReactiveFormsModule } from '@angular/forms';
|
||
import { MatCardModule } from '@angular/material/card';
|
||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||
import { MatInputModule } from '@angular/material/input';
|
||
import { MatButtonModule } from '@angular/material/button';
|
||
import { MatIconModule } from '@angular/material/icon';
|
||
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
||
import { RouterModule } from '@angular/router';
|
||
|
||
@Component({
|
||
selector: 'app-login',
|
||
templateUrl: './login.component.html',
|
||
styleUrls: ['./login.component.scss'],
|
||
standalone: true,
|
||
imports: [
|
||
CommonModule,
|
||
ReactiveFormsModule,
|
||
MatCardModule,
|
||
MatFormFieldModule,
|
||
MatInputModule,
|
||
MatButtonModule,
|
||
MatIconModule,
|
||
MatProgressSpinnerModule,
|
||
RouterModule
|
||
]
|
||
})
|
||
export class LoginComponent implements OnInit {
|
||
loginForm: FormGroup;
|
||
isLoading = false;
|
||
hidePassword = true;
|
||
|
||
constructor(
|
||
private fb: FormBuilder,
|
||
private authService: AuthService,
|
||
private router: Router,
|
||
private snackBar: MatSnackBar
|
||
) {
|
||
this.loginForm = this.fb.group({
|
||
email: ['', [Validators.required, Validators.email]],
|
||
password: ['', [Validators.required]]
|
||
});
|
||
}
|
||
|
||
ngOnInit(): void {
|
||
// Eğer kullanıcı zaten giriş yapmışsa ana sayfaya yönlendir
|
||
this.authService.isAuthenticated$.subscribe(
|
||
isAuthenticated => {
|
||
if (isAuthenticated) {
|
||
this.router.navigate(['/']);
|
||
}
|
||
}
|
||
);
|
||
}
|
||
|
||
onSubmit(): void {
|
||
if (this.loginForm.valid) {
|
||
this.isLoading = true;
|
||
const { email, password } = this.loginForm.value;
|
||
|
||
this.authService.login(email, password).subscribe(
|
||
() => {
|
||
this.isLoading = false;
|
||
this.router.navigate(['/']);
|
||
this.snackBar.open('Login successful!', 'Close', { duration: 3000 });
|
||
},
|
||
error => {
|
||
this.isLoading = false;
|
||
this.snackBar.open(error.message || 'Login failed', 'Close', { duration: 3000 });
|
||
}
|
||
);
|
||
}
|
||
}
|
||
}
|