register.page.ts 1.92 KB
Newer Older
gap95's avatar
gap95 committed
1
2
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
3
import { HttpClient, HttpHeaders } from '@angular/common/http';
gap95's avatar
gap95 committed
4
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;
19
20


21
22
23
24
25
26
27
28
  constructor(private router: Router, 
    public httpClient: HttpClient, 
    public restService: RestService,
    private toastService: ToastService,
    private fb: FormBuilder) {
      this.createForm();

     }
gap95's avatar
gap95 committed
29
30

  ngOnInit() {
31
32
33
  }
  createForm() {
    this.angForm = this.fb.group({
34
35
36
37
38
       
       email: ['',[ Validators.required, Validators.email,Validators.pattern(/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/)]],
       password: ['', [Validators.required ]],
       firstname: ['',[ Validators.required ]],
       lastname: ['', [Validators.required ]],
39
40
41
42
43
       
 
   
    });
    
gap95's avatar
gap95 committed
44
  }
gap95's avatar
gap95 committed
45
  submitRegister() {
46
47
48
    if (this.angForm.invalid) {
      return;
  }
49
50
51
  let Form = JSON.stringify(this.angForm.value);
  const headers = new HttpHeaders().set("Content-Type", 'application/json');
    this.registerApi = this.httpClient.post('http://193.196.52.237:8081/register', Form,{headers});
gap95's avatar
gap95 committed
52
53
    this.registerApi
      .subscribe((data) => {
54
        //console.log('my data: ', data);
gap95's avatar
gap95 committed
55
        this.restService.setToken(data.token);
56
        this.toastService.showToast("Registration Successful!")
gap95's avatar
gap95 committed
57
        this.router.navigateByUrl('/login');
58
        
gap95's avatar
gap95 committed
59
60
      }, (error) => {
        console.log(error);
61
62
        this.toastService.showToast("Registration failed!")
        
gap95's avatar
gap95 committed
63
64
      });
  }
65
66
67
  login(){
    this.router.navigateByUrl('/login');
  }
gap95's avatar
gap95 committed
68
}