Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, NgModule, OnInit } from '@angular/core';
- import { Router, ActivatedRoute } from '@angular/router';
- import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
- import { first } from 'rxjs/operators';
- import { AlertService, AuthenticationService } from '../../../_services';
- @Component({
- selector: 'app-log-in',
- templateUrl: './log-in.component.html',
- styleUrls: ['./log-in.component.scss']
- })
- export class LogInComponent implements OnInit {
- loginForm: FormGroup;
- loading = false;
- submitted = false;
- returnUrl: string;
- constructor(
- private formBuilder: FormBuilder,
- private route: ActivatedRoute,
- private router: Router,
- private authenticationService: AuthenticationService,
- private alertService: AlertService) {}
- ngOnInit() {
- this.loginForm = this.formBuilder.group({
- username: ['', Validators.required],
- password: ['', Validators.required]
- });
- // reset login status
- this.authenticationService.logout();
- // get return url from route parameters or default to '/'
- this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
- }
- onSubmit() {
- this.submitted = true;
- // stop here if form is invalid
- if (this.loginForm.invalid) {
- return;
- }
- this.loading = true;
- this.authenticationService.login(
- this.loginForm.controls.username.value,
- this.loginForm.controls.password.value
- )
- .pipe(first())
- .subscribe(
- data => {
- this.router.navigate([this.returnUrl]);
- },
- error => {
- this.alertService.error(error);
- this.loading = false;
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement