44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { FormGroup, FormControl, Validators } from '@angular/forms';
|
|
import { UserService } from '../../services/user.service';
|
|
import { Auth } from '../../interfaces/auth.interface';
|
|
import { Router } from '@angular/router';
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
templateUrl: './login.component.html',
|
|
styleUrls: ['./login.component.scss']
|
|
})
|
|
export class LoginComponent implements OnInit {
|
|
|
|
loginForm: FormGroup;
|
|
|
|
constructor(private userSerice: UserService,
|
|
private router: Router) { }
|
|
|
|
ngOnInit(): void {
|
|
this.loginForm = this.createLoginForm();
|
|
}
|
|
|
|
|
|
createLoginForm(): FormGroup {
|
|
return new FormGroup({
|
|
userName: new FormControl('', Validators.required),
|
|
fullName: new FormControl('', Validators.required),
|
|
password: new FormControl('', Validators.required),
|
|
repeatedPassword: new FormControl('', Validators.required)
|
|
});
|
|
}
|
|
|
|
onLoginUser(): void {
|
|
this.userSerice.login(this.loginForm.value).subscribe((auth: Auth) => {
|
|
localStorage.setItem('token', auth.token);
|
|
localStorage.setItem('userId', auth.user.id);
|
|
localStorage.setItem('userName', auth.user.userName);
|
|
localStorage.setItem('fullName', auth.user.fullName);
|
|
this.router.navigateByUrl('/group');
|
|
});
|
|
}
|
|
|
|
}
|