<commit_before>// defaults
import {Component, OnInit} from '@angular/core';

// custom
import {LoginService} from '../../services/login.service';

// router
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {User} from '../../models/user';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  loginFailed = false;
  currentUserName;
  message;
  loginForm: FormGroup;
  isLoggedIn = false;
  user: User;
  isLoginUserNameValid = true;
  isLoginPasswordValid = true;

  constructor(private loginService: LoginService,
              private fb: FormBuilder) {
  }

  ngOnInit() {
    this.createForm();
  }

  /**
   * creates the form using form builder
   * @returns {FormGroup}
   */
  createForm(): FormGroup {
    // group the form elements using the form builder
    return this.loginForm = this.fb.group({
      username: ['', [Validators.required, Validators.minLength(6)]],
      password: ['', [Validators.required, Validators.minLength(8)]]
    });
  }

  /**
   * submit user credentials to the server
   */
  onSubmit(): void {
    // validation checks before submit
    if (!this.validateLoginUserName()) { return; }
    if (!this.validateLoginPassword()) { return; }

    // subscribe to the login service
    this.loginService.login(this.loginForm.value).subscribe(userData => {
        // login successful, save token to local storage
        this.user = new User(userData.token, this.loginForm.get('username').value);
        this.setUserSession(this.user);

        this.isLoggedIn = true;

        localStorage.setItem('isLoggedIn', 'true');

        this.currentUserName = this.user.username;

        this.loginForm.reset();
      },
      error => {
        // login attempt failed
        console.log(error);
        this.loginFailed = true;
        this.message = error.message || 'Something went wrong';
      });
  }

  /**
   * validates username field
   * @returns {boolean}
   */
  validateLoginUserName(): boolean {
    if (this.loginForm.controls['username'].valid) {
      return this.isLoginUserNameValid = true;
    } else {
      return this.isLoginUserNameValid = false;
    }
  }

  /**
   * validates password field
   * @returns {boolean}
   */
  validateLoginPassword(): boolean {
    if (this.loginForm.controls['password'].valid) {
      return this.isLoginPasswordValid = true;
    } else {<commit_msg>chore: added alternative login failure messages<commit_after>// defaults
import {Component, OnInit} from '@angular/core';
// custom
import {LoginService} from '../../services/login.service';
// router
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {User} from '../../models/user';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  loginFailed = false;
  currentUserName;
  message;
  loginForm: FormGroup;
  isLoggedIn = false;
  user: User;
  isLoginUserNameValid = true;
  isLoginPasswordValid = true;

  constructor(private loginService: LoginService,
              private fb: FormBuilder) {
  }

  ngOnInit() {
    this.createForm();
  }

  /**
   * creates the form using form builder
   * @returns {FormGroup}
   */
  createForm(): FormGroup {
    // group the form elements using the form builder
    return this.loginForm = this.fb.group({
      username: ['', [Validators.required, Validators.minLength(6)]],
      password: ['', [Validators.required, Validators.minLength(8)]]
    });
  }

  /**
   * submit user credentials to the server
   */
  onSubmit(): void {
    // validation checks before submit
    if (!this.validateLoginUserName()) { return; }
    if (!this.validateLoginPassword()) { return; }

    // subscribe to the login service
    this.loginService.login(this.loginForm.value).subscribe(userData => {
        // login successful, save token to local storage
        this.user = new User(userData.token, this.loginForm.get('username').value);
        this.setUserSession(this.user);

        this.isLoggedIn = true;

        localStorage.setItem('isLoggedIn', 'true');

        this.currentUserName = this.user.username;

        this.loginForm.reset();
      },
      error => {
        // login attempt failed
        console.log(error);
        this.loginFailed = true;
        this.message = error.message || 'Login credentials incorrect';
      });
  }

  /**
   * validates username field
   * @returns {boolean}
   */
  validateLoginUserName(): boolean {
    if (this.loginForm.controls['username'].valid) {
      return this.isLoginUserNameValid = true;
    } else {
      return this.isLoginUserNameValid = false;
    }
  }

  /**
   * validates password field
   * @returns {boolean}
   */
  validateLoginPassword(): boolean {
    if (this.loginForm.controls['password'].valid) {
      return this.isLoginPasswordValid = true;
    } else {
{'fraction_non_alphanumeric': 0.1345425553119394, 'fraction_numerical': 0.0007972892166633447, 'mean_word_length': 2.9449685534591197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '122892', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1437, 'n_words': 341}