/** ============LICENSE_START======================================================= Copyright (C) 2019 Samsung. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SPDX-License-Identifier: Apache-2.0 ============LICENSE_END========================================================= @authors: k.kazak@samsung.com **/ import { Component, OnInit } from '@angular/core'; import {FormBuilder, FormGroup, Validators, ReactiveFormsModule} from "@angular/forms"; import {ActivatedRoute, Router} from "@angular/router"; import {AuthenticationService} from "../authentication.service"; import {first} from "rxjs/internal/operators"; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: [] }) export class LoginComponent implements OnInit { loginForm: FormGroup; loading = false; submitted = false; returnUrl: string; error = ''; constructor(private formBuilder: FormBuilder, private route: ActivatedRoute, private router: Router, private authenticationService: AuthenticationService) { } ngOnInit() { this.loginForm = this.formBuilder.group({ username: ['', Validators.required], password: ['', Validators.required] }); // logout this.authenticationService.logout(); this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; } // convenience getter for easy access to form fields get f() { return this.loginForm.controls; } onSubmit() { this.submitted = true; // stop here if form is invalid if (this.loginForm.invalid) { return; } this.loading = true; this.authenticationService.login(this.f.username.value, this.f.password.value) .subscribe( next => { this.router.navigate([this.returnUrl]); }, error => { this.error = error; this.loading = false; }); } }