|
1 | 1 | import { Component, OnInit } from '@angular/core';
|
2 | 2 | import { MatDialog } from '@angular/material';
|
3 | 3 | import { Router } from '@angular/router';
|
4 |
| -import { FormGroup, FormControl, Validators } from '@angular/forms'; |
| 4 | +import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms'; |
5 | 5 | import { AuthService } from '../auth.service';
|
| 6 | +import { User } from '../user.model'; |
| 7 | +import { HomeComponent } from 'src/app/view/home/home.component'; |
6 | 8 |
|
7 | 9 | @Component({
|
8 | 10 | selector: 'app-login',
|
9 | 11 | templateUrl: './login.component.html',
|
10 | 12 | styleUrls: ['./login.component.css']
|
11 | 13 | })
|
12 | 14 | export class LoginComponent implements OnInit {
|
13 |
| - public email = ''; |
14 |
| - public password = ''; |
| 15 | + |
| 16 | + registerForm: FormGroup; |
| 17 | + loading = false; |
| 18 | + submitted = false; |
15 | 19 |
|
16 | 20 | loginForm: FormGroup;
|
17 |
| - constructor(private authService: AuthService) { } |
18 |
| - |
19 |
| -onSubmit(event) { |
20 |
| - event.preventDefault(); |
21 |
| - console.log('submit done'); |
22 |
| -// this.authService.login({ |
23 |
| -// email: this.loginForm.value.email, |
24 |
| -// password: this.loginForm.value.password |
25 |
| -// }); |
| 21 | + constructor(private authService: AuthService, |
| 22 | + private formBuilder: FormBuilder, |
| 23 | + private router: Router) { |
| 24 | + this.registerForm = this.formBuilder.group({ |
| 25 | + username: ['', Validators.required], |
| 26 | + password: ['', [Validators.required, Validators.minLength(6)]], |
| 27 | + |
| 28 | + }); |
| 29 | + } |
| 30 | + get f() { return this.registerForm.controls; } |
| 31 | + |
| 32 | +onSubmit() { |
| 33 | + this.submitted = true; |
| 34 | + |
| 35 | + // stop here if form is invalid |
| 36 | + if (this.registerForm.invalid) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + this.loading = true; |
| 41 | + var user = new User(); |
| 42 | + // this.userService.register(this.registerForm.value) |
| 43 | + // .pipe(first()) |
| 44 | + // .subscribe( |
| 45 | + // data => { |
| 46 | + // this.alertService.success('Registration successful', true); |
| 47 | + // this.router.navigate(['/login']); |
| 48 | + // }, |
| 49 | + // error => { |
| 50 | + // this.alertService.error(error); |
| 51 | + // this.loading = false; |
| 52 | + // }); |
| 53 | +console.log(this.registerForm.get('username').value) |
| 54 | + |
| 55 | +if(this.registerForm.get('username').value=='admin' && this.registerForm.get('password').value == 'test1234'){ |
| 56 | + console.log('Successful login'); |
| 57 | + this.authService.login(); |
| 58 | + this.router.navigate(['home']); |
| 59 | + |
| 60 | +}else{ |
| 61 | +console.log("invalid login"); |
| 62 | +} |
26 | 63 |
|
27 | 64 | }
|
28 | 65 |
|
|
0 commit comments