Website Summary Files Commits Branches @commit @diff @stats
path: root / src / DevHive.Angular / src / app / components / register / register.component.ts
blob: 73045893049a833a75367331c5aa93a0e065b872 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Title } from '@angular/platform-browser';
import { Router } from '@angular/router';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  public registerUserFormGroup: FormGroup;

  private _title = 'Register';

  constructor(private titleService: Title, private fb: FormBuilder, private router: Router) {
    titleService.setTitle(this._title);
  }

  ngOnInit(): void {
    this.registerUserFormGroup = this.fb.group({
      firstName: new FormControl('', [
        Validators.required,
        Validators.minLength(3)
      ]),
      lastName: new FormControl('', [
        Validators.required,
        Validators.minLength(3)
      ]),
      username: new FormControl('', [
        Validators.required,
        Validators.minLength(3)
      ]),
      email: new FormControl('', [
        Validators.required,
        Validators.email,
      ]),
      password: new FormControl('', [
        Validators.required,
        Validators.minLength(3),
        Validators.pattern('.*[0-9].*') // Check if password contains atleast one number
      ]),
    });

    this.registerUserFormGroup.valueChanges.subscribe(console.log);
  }

  async onSubmit(): Promise<void> {
    // TODO: add a check for form data validity

    const response = await fetch('http://localhost:5000/api/User/register', {
      method: 'POST',
      body: JSON.stringify({
        UserName: this.registerUserFormGroup.get('username')?.value,
        Email: this.registerUserFormGroup.get('email')?.value,
        FirstName: this.registerUserFormGroup.get('firstName')?.value,
        LastName: this.registerUserFormGroup.get('lastName')?.value,
        Password: this.registerUserFormGroup.get('password')?.value
      }),
      headers: {
        'Content-Type': 'application/json'
      }
    });
    const userCred: string = await response.json();

    // TODO: don't redirect if there is an error response
    sessionStorage.setItem('UserCred', JSON.stringify(userCred));
    this.router.navigate(['/']);
  }

  onRedirectRegister(): void {
    this.router.navigate(['/login']);
  }

  get firstName() {
    return this.registerUserFormGroup.get('firstName');
  }

  get lastName() {
    return this.registerUserFormGroup.get('lastName');
  }

  get username() {
    return this.registerUserFormGroup.get('username');
  }

  get email() {
    return this.registerUserFormGroup.get('email');
  }

  get password() {
    return this.registerUserFormGroup.get('password');
  }
}