timemanager/frontend/src/app/auth/registration/registration.component.ts

85 lines
2.7 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import {MatDialog as MatDialog, MatDialogRef as MatDialogRef} from "@angular/material/dialog";
import {LoginService} from "../../../api";
import {Router} from "@angular/router";
import {MatSnackBar as MatSnackBar} from "@angular/material/snack-bar";
import {AuthService} from "../../auth.service";
import {LoginComponent} from "../login/login.component";
import {LoginDialogServiceService} from "../../login-dialog-service.service";
import {AbstractControl, UntypedFormControl, ValidationErrors, Validators} from "@angular/forms";
@Component({
selector: 'app-registration',
templateUrl: './registration.component.html',
styleUrls: ['./registration.component.css']
})
export class RegistrationComponent implements OnInit {
username: string = "";
email: string = "";
password: string = "";
hide: boolean = true;
pending: boolean = false;
isAuth: boolean = false;
usernameControl = new UntypedFormControl('', [Validators.required, Validators.minLength(3)]);
emailControl = new UntypedFormControl('', [Validators.required, Validators.email])
passwordControl = new UntypedFormControl('', [Validators.required, Validators.minLength(6)]);
passwordDuplicateControl = new UntypedFormControl('', [
Validators.required,
Validators.minLength(6),
(control: AbstractControl): ValidationErrors | null => {
return control.value != this.password ? {duplicate: {value: control.value}} : null
}
])
constructor(
public dialogRef: MatDialogRef<RegistrationComponent>,
private loginService: LoginService,
private authService: AuthService,
private router: Router,
private snackbar: MatSnackBar,
private loginDialogService: LoginDialogServiceService
) { }
ngOnInit(): void {
this.isAuth = this.authService.hasKey();
}
onBtnRegistrate() {
this.pending = true;
this.loginService.registrate({
username: this.username,
password: this.password,
email: this.email
}).subscribe({
next: resp => {
if(resp.status=="success") {
this.snackbar.open("Registration succeed!", "", {duration: 2000})
this.dialogRef.close();
}
this.pending = false
},
error : err => {
if(err.status == 403) {
this.snackbar.open("No permission for Sign Up!", "", {duration: 2000})
} else if(err.status == 409) {
this.snackbar.open("This username or email-Adress is already in use!", "", {duration: 2000})
}
this.pending = false;
}
})
}
onClose() {
this.dialogRef.close();
}
onChangeAuthMode() {
this.dialogRef.close();
this.loginDialogService.openLoginDialog();
}
}