Split de la page de login en 2 composants

This commit is contained in:
Sébastien André
2020-03-25 11:26:18 +01:00
parent cedbae0f22
commit aa959578dd
12 changed files with 291 additions and 144 deletions

View File

@@ -0,0 +1,80 @@
import { Component, OnInit } from "@angular/core";
import { Router, ActivatedRoute } from "@angular/router";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { first } from "rxjs/operators";
import { AuthenticationService } from "../../services/authentication.service";
import { User } from "../../models/user";
@Component({
selector: "app-create-user",
templateUrl: "./create-user.component.html",
styleUrls: ["./create-user.component.css"]
})
export class CreateUserComponent implements OnInit {
createForm: FormGroup;
loading = false;
submitted = false;
returnUrl: string;
error = "";
constructor(
private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private authenticationService: AuthenticationService
) {
// redirect to home if already logged in
if (this.authenticationService.currentUserValue) {
this.router.navigate(["/"]);
}
}
ngOnInit() {
this.createForm = this.formBuilder.group({
username: ["", Validators.required],
password: ["", Validators.required],
firstname: ["", Validators.required],
lastname: ["", Validators.required],
email: ["", Validators.required]
});
// get return url from route parameters or default to '/'
this.returnUrl = this.route.snapshot.queryParams["returnUrl"] || "/";
}
get createCtrls() {
return this.createForm.controls;
}
onCreateSubmit() {
this.submitted = true;
// stop here if form is invalid
if (this.createForm.invalid) {
return;
}
this.loading = true;
let createUser = new User();
createUser.login = this.createCtrls.username.value;
createUser.password = this.createCtrls.password.value;
createUser.firstName = this.createCtrls.firstname.value;
createUser.lastName = this.createCtrls.lastname.value;
createUser.email = this.createCtrls.email.value;
this.authenticationService
.create(createUser)
.pipe(first())
.subscribe(
data => {
this.router.navigate([this.returnUrl]);
},
error => {
this.error = error;
this.loading = false;
}
);
}
}