72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { LoginService } from '../../core/auth/login.service';
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
import { Validators, UntypedFormGroup, UntypedFormBuilder } from '@angular/forms';
|
|
import { first } from 'rxjs/operators';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
templateUrl: './login.component.html',
|
|
styleUrls: ['./login.component.scss'],
|
|
})
|
|
export class LoginComponent implements OnInit {
|
|
loginForm: UntypedFormGroup;
|
|
returnUrl: string;
|
|
loading = false;
|
|
submitted = false;
|
|
hide = true;
|
|
|
|
constructor(
|
|
private loginService: LoginService,
|
|
private router: Router,
|
|
private route: ActivatedRoute,
|
|
private formBuilder: UntypedFormBuilder,
|
|
private snackBar: MatSnackBar
|
|
) {
|
|
if (this.loginService.currentHoodAdminValue) {
|
|
this.router.navigate(['/dashboard']);
|
|
}
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.loginForm = this.formBuilder.group({
|
|
email: ['', Validators.required],
|
|
password: ['', Validators.required],
|
|
});
|
|
this.returnUrl = this.route.snapshot.queryParams.returnUrl || '/dashboard';
|
|
|
|
if (this.route.snapshot.queryParams.registered === true) {
|
|
this.snackBar.open('Registration successful', 'Close', {
|
|
duration: 2000,
|
|
});
|
|
}
|
|
}
|
|
|
|
onSubmit() {
|
|
this.submitted = true;
|
|
if (this.loginForm.invalid) {
|
|
return;
|
|
}
|
|
|
|
this.loading = true;
|
|
this.loginService
|
|
.login(
|
|
this.loginForm.controls.email.value,
|
|
this.loginForm.controls.password.value
|
|
)
|
|
.pipe(first())
|
|
.subscribe(
|
|
(data) => {
|
|
this.router.navigate([this.returnUrl]);
|
|
},
|
|
(error) => {
|
|
this.snackBar.open('Wrong credentials! Try again.', 'Close', {
|
|
duration: 2000,
|
|
});
|
|
this.loading = false;
|
|
}
|
|
);
|
|
}
|
|
}
|