register.page.ts 1.87 KB
Newer Older
gap95's avatar
gap95 committed
1
2
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
gap95's avatar
gap95 committed
3
4
import { HttpClient } from '@angular/common/http';
import { RestService } from 'src/app/rest.service';
5
import { ToastService } from '../../services/toast.service';
gap95's avatar
gap95 committed
6
import { Router } from '@angular/router';
7
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
gap95's avatar
gap95 committed
8
9
10
11
12
13
14

@Component({
  selector: 'app-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {
15
  public angForm: FormGroup;
gap95's avatar
gap95 committed
16
  registerApi: Observable<any>;
gap95's avatar
gap95 committed
17
 
gap95's avatar
gap95 committed
18
  correctCredentials: boolean;
gap95's avatar
gap95 committed
19
20
21
22
  email: "";
  password: "";
  lastName: "";
  firstName: "";
23
24
25
26
27
28
29
30
31
  
  constructor(private router: Router, 
    public httpClient: HttpClient, 
    public restService: RestService,
    private toastService: ToastService,
    private fb: FormBuilder) {
      this.createForm();

     }
gap95's avatar
gap95 committed
32
33

  ngOnInit() {
34
35
36
37
38
39
40
41
42
43
44
45
  }
  createForm() {
    this.angForm = this.fb.group({
       firstName: ['',[ Validators.required ]],
       lastName: ['', [Validators.required ]],
       email: ['',[ Validators.required, Validators.email]],
       password: ['', [Validators.required,Validators.minLength(4) ]],
       
 
   
    });
    
gap95's avatar
gap95 committed
46
  }
gap95's avatar
gap95 committed
47
  submitRegister() {
48
49
50
51
    if (this.angForm.invalid) {
      return;
  }

gap95's avatar
gap95 committed
52
53
54
55
56
57
58
59
60
61
    this.registerApi = this.httpClient.post('http://193.196.52.237:8081/register', {
      "email": this.email,
      "password": this.password,
      "firstname": this.firstName,
      "lastname": this.lastName
    });
    this.registerApi
      .subscribe((data) => {
        console.log('my data: ', data);
        this.restService.setToken(data.token);
62
        this.toastService.showToast("Registration Successful!")
gap95's avatar
gap95 committed
63
        this.router.navigateByUrl('/login');
64
        
gap95's avatar
gap95 committed
65
66
      }, (error) => {
        console.log(error);
67
68
        this.toastService.showToast("Registration failed!")
        
gap95's avatar
gap95 committed
69
70
      });
  }
gap95's avatar
gap95 committed
71
}