76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import { Component, OnInit } from "@angular/core";
|
|
import { FormBuilder, FormControl, FormGroup, Validators } from "@angular/forms";
|
|
import { UserData } from "src/app/interface/data";
|
|
import { JsonrpcService, RpcService } from "src/app/services/jsonrpc.service";
|
|
import { MessageService } from "primeng/api";
|
|
import { CookiesService } from "src/app/services/cookies.service";
|
|
import { DynamicDialogRef } from "primeng/dynamicdialog";
|
|
|
|
|
|
@Component({
|
|
selector: 'app-login-modal',
|
|
templateUrl: './login-modal.component.html',
|
|
styleUrls: ['./login-modal.component.scss'],
|
|
})
|
|
export class LoginModalComponent implements OnInit {
|
|
public mainFormGroup!: FormGroup;
|
|
public UserData: UserData = {
|
|
login: '',
|
|
password: ''
|
|
}
|
|
public errorConfirm: boolean = false;
|
|
|
|
|
|
constructor(
|
|
private fb: FormBuilder,
|
|
private jsonRpcService: JsonrpcService,
|
|
private messageService: MessageService,
|
|
private cookiesService: CookiesService,
|
|
private dialogRef: DynamicDialogRef
|
|
) {}
|
|
|
|
ngOnInit(): void {
|
|
this.mainFormGroup = new FormGroup({
|
|
login: new FormControl('', [Validators.required, Validators.minLength(4)]),
|
|
password: new FormControl('', [Validators.required, Validators.minLength(4)]),
|
|
})
|
|
}
|
|
|
|
submit() {
|
|
const mainControls = this.mainFormGroup.controls;
|
|
if (this.mainFormGroup.invalid) {
|
|
Object.keys(mainControls).forEach(groupName => {
|
|
mainControls[groupName].markAsTouched();
|
|
});
|
|
return;
|
|
}
|
|
this.submitAuth()
|
|
}
|
|
|
|
submitAuth() {
|
|
const userData = this.mainFormGroup.value;
|
|
this.jsonRpcService.rpc({
|
|
method: 'login',
|
|
params: [userData.login, userData.password]
|
|
}, RpcService.authService, false).subscribe({
|
|
next: (result) => {
|
|
this.cookiesService.setCookie('token', result);
|
|
this.messageService.add({
|
|
severity: 'success',
|
|
summary: 'Авторизация прошла успешно!',
|
|
})
|
|
this.dialogRef.close()
|
|
},
|
|
error: (err) => {
|
|
console.log('ERROR: ', err)
|
|
this.messageService.add({
|
|
severity: 'error',
|
|
summary: 'Произошла ошибка!',
|
|
})
|
|
this.errorConfirm = true;
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
} |