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

@@ -1,11 +1,4 @@
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-login",
@@ -13,94 +6,7 @@ import { User } from "../../models/user";
styleUrls: ["./login.component.css"]
})
export class LoginComponent implements OnInit {
loginForm: FormGroup;
createForm: FormGroup;
loading = false;
submitted = false;
returnUrl: string;
error = "";
constructor() {}
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.loginForm = this.formBuilder.group({
username: ["", Validators.required],
password: ["", Validators.required]
});
this.createForm = this.formBuilder.group({
username: ["", Validators.required],
password: ["", Validators.required]
});
// get return url from route parameters or default to '/'
this.returnUrl = this.route.snapshot.queryParams["returnUrl"] || "/";
}
get loginCtrls() {
return this.loginForm.controls;
}
get createCtrls() {
return this.createForm.controls;
}
onLoginSubmit() {
this.submitted = true;
// stop here if form is invalid
if (this.loginForm.invalid) {
return;
}
this.loading = true;
this.authenticationService
.login(this.loginCtrls.username.value, this.loginCtrls.password.value)
.pipe(first())
.subscribe(
data => {
this.router.navigate([this.returnUrl]);
},
error => {
this.error = error;
this.loading = false;
}
);
}
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;
this.authenticationService
.create(createUser)
.pipe(first())
.subscribe(
data => {
this.router.navigate([this.returnUrl]);
},
error => {
this.error = error;
this.loading = false;
}
);
}
ngOnInit() {}
}