import {Component, Inject, OnInit} from '@angular/core'; import {LoginService, PropertiesService} from "../../../api"; import {MAT_DIALOG_DATA as MAT_DIALOG_DATA, MatDialog as MatDialog, MatDialogRef as MatDialogRef} from "@angular/material/dialog"; import {Router} from "@angular/router"; import {MatSnackBar as MatSnackBar} from "@angular/material/snack-bar"; import {AuthService} from "../../auth.service"; import {RegistrationComponent} from "../registration/registration.component"; export enum LoginInfoTypes { SESSION_INVALID } @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit { username: string = ""; password: string = ""; isAuth: boolean = false; isRegistrationEnabled: boolean = true hide: boolean = true; infotypes = LoginInfoTypes; pending: boolean = false; constructor( public dialogRef: MatDialogRef, private loginService: LoginService, private authService: AuthService, private propertyService: PropertiesService, private router: Router, private snackbar: MatSnackBar, @Inject(MAT_DIALOG_DATA) public additionalInfo: LoginInfoTypes[], private dialog: MatDialog ) { } ngOnInit(): void { this.isAuth = this.authService.hasKey(); this.propertyService.propertiesSignupGet().subscribe({ next: resp => { if(resp.property == "REGISTRATION_PROPERTY_NAME") { this.isRegistrationEnabled = resp.status; } } }) } onBtnLogin() { this.pending = true this.loginService.login({ username: this.username, password: this.password }).subscribe({ next: data => { if(data.status == "successful") { console.log("Token: " + this.authService.getAccessToken()) this.authService.setAccessToken(data.jwt!!) console.log("Token: " + this.authService.getAccessToken()) console.log("Admin: " + this.authService.isAdmin()) //close on authorized this.dialogRef.close(); this.snackbar.open("Login successful", "", {duration: 2000}) this.pending = false; } }, error: err => { if(err.status == 401) { this.snackbar.open("Invalid username or password", "", {duration: 2000}) this.pending = false; } } }) } onClose() { this.dialogRef.close(); } onChangeAuthMode() { this.dialogRef.close(); const registrationDialog = this.dialog.open(RegistrationComponent, {}); return registrationDialog.afterClosed(); } }