Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class LoginComponent {
- credentials: LoginRequest;
- error = '';
- formGroup: FormGroup;
- constructor(private auth: AuthService, private router: Router) {
- }
- ngOnInit() {
- this.formGroup = new FormGroup({
- username: new FormControl("", Validators.required),
- password: new FormControl("", Validators.required)
- });
- this.credentials = new LoginRequest();
- this.auth.logout();
- }
- login(): Observable<boolean> {
- if (this.formGroup.invalid) {
- this.markControls(this.formGroup, control => control.markAsDirty());
- console.warn("Cannot login, formGroup is invalid!");
- return;
- }
- const loggedUser: LoggedUser = {
- username: this.formGroup.value.username
- };
- try
- {
- this.auth.login(this.formGroup.value)
- .subscribe(
- result => {
- if (result === true) {
- this.router.navigate(['/']);
- console.log("Login success!");
- }
- });
- }
- catch (ex)
- {
- this.error = 'Nieprawidłowy login lub hasło!';
- }
- }
- private markControls(form: FormGroup, callback: (control: AbstractControl) => void): void {
- Object.keys(form.controls)
- .forEach(key => {
- callback(form.get(key));
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement