aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_modals/login-modal/login-modal.component.ts
blob: 062a0550a096778cdb54f2a866101cc2d308c5c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { AuthService } from 'src/app/_services/auth.service';
import { UserInfoService } from 'src/app/_services/user-info.service';
import shared from '../../Shared';
import {AfterViewInit, ElementRef} from '@angular/core';

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

  @ViewChild('closeButton') closeButton?: ElementRef;
  @ViewChild('pass') passwordInput!: ElementRef;

  username: string = '';
  password: string = '';

  passwordShown: boolean = false;

  wrongCreds: boolean = false;

  constructor(
    private authService: AuthService,
    private cookie: CookieService,
    private router: Router,
    private userInfoService: UserInfoService
  ) { }

  ngOnInit(): void {
  }

  doLogin() {
    if (this.username.length > 0 && this.password.length > 0) {
      this.authService.login(this.username, this.password).subscribe((response) => {

        if (response == "Username doesn't exist" || response == "Wrong password") {
          this.wrongCreds = true;
          this.password = '';
          this.passwordShown = false;
          this.passwordInput.nativeElement.type = "password";
        }
        else {
          this.wrongCreds = false;
          this.authService.authenticate(response);
          (<HTMLSelectElement>this.closeButton?.nativeElement).click();
          this.userInfoService.getUserInfo().subscribe((response) => {
            shared.photoId = response.photoId;
          });
        }
      });
    }
    else {
      this.wrongCreds = true;
      this.password = '';
    }
  }
  resetData() {
    this.wrongCreds = false;
    this.username = '';
    this.password = '';
  }

  togglePasswordShown() {
    this.passwordShown = !this.passwordShown;

    if (this.passwordShown)
      this.passwordInput.nativeElement.type = "text";
    else 
      this.passwordInput.nativeElement.type = "password";
  }

  cleanWarnings() {
    this.wrongCreds = false;
  }
}