From d4c6b40bb3d58c9ec3c73d72d5ca3c6db40c41da Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 8 Jan 2021 15:34:52 +0200 Subject: Implemented and improved sending API requests from login and register pages; the response is printed in the (browser) console --- .../src/app/components/login/login.component.html | 4 ++-- .../src/app/components/login/login.component.ts | 28 ++++++++++++++-------- .../components/register/register.component.html | 8 +++---- .../app/components/register/register.component.ts | 20 ++++++++++++++-- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/DevHive.Angular/src/app/components/login/login.component.html b/src/DevHive.Angular/src/app/components/login/login.component.html index 29cd27e..c33f493 100644 --- a/src/DevHive.Angular/src/app/components/login/login.component.html +++ b/src/DevHive.Angular/src/app/components/login/login.component.html @@ -3,8 +3,8 @@ Login
-
- + +
diff --git a/src/DevHive.Angular/src/app/components/login/login.component.ts b/src/DevHive.Angular/src/app/components/login/login.component.ts index 140a852..fb90837 100644 --- a/src/DevHive.Angular/src/app/components/login/login.component.ts +++ b/src/DevHive.Angular/src/app/components/login/login.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { FormGroup, FormControl, FormBuilder } from '@angular/forms'; +import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { Title } from '@angular/platform-browser'; @Component({ @@ -8,7 +8,7 @@ import { Title } from '@angular/platform-browser'; styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit { - loginForm: FormGroup; + loginUserFormGroup: FormGroup; private _title = 'Login'; @@ -17,20 +17,28 @@ export class LoginComponent implements OnInit { } ngOnInit(): void { - this.loginForm = this.fb.group({ - userName: '', - password: '' + this.loginUserFormGroup = this.fb.group({ + username: ['', [ + Validators.required, + Validators.minLength(3) + ]], + password: ['', [ + Validators.required, + // Add password pattern + ]] }); } - async onSubmit(): Promise { - const response = await fetch('http://localhost:5000/api/User/login', { + onSubmit(): void { + fetch('http://localhost:5000/api/User/login', { method: 'POST', - body: `{"UserName": "${this.loginForm.get('userName')?.value}", "Password": "${this.loginForm.get('password')?.value}"}`, + body: `{ + "UserName": "${this.loginUserFormGroup.get('username')?.value}", + "Password": "${this.loginUserFormGroup.get('password')?.value}" + }`, headers: { 'Content-Type': 'application/json' } - }); - console.log(response); + }).then(response => response.json()).then(data => { console.log(data); }); } } diff --git a/src/DevHive.Angular/src/app/components/register/register.component.html b/src/DevHive.Angular/src/app/components/register/register.component.html index e394ace..de79698 100644 --- a/src/DevHive.Angular/src/app/components/register/register.component.html +++ b/src/DevHive.Angular/src/app/components/register/register.component.html @@ -1,11 +1,11 @@
Register
- - + +
Value: {{ registerUserFormGroup.value | json }}
- + @@ -15,4 +15,4 @@
-
\ No newline at end of file + diff --git a/src/DevHive.Angular/src/app/components/register/register.component.ts b/src/DevHive.Angular/src/app/components/register/register.component.ts index dee928a..3226833 100644 --- a/src/DevHive.Angular/src/app/components/register/register.component.ts +++ b/src/DevHive.Angular/src/app/components/register/register.component.ts @@ -41,13 +41,29 @@ export class RegisterComponent implements OnInit { ]], password: ['', [ Validators.required, - //Add password pattern + // Add password pattern ]], }); this.registerUserFormGroup.valueChanges.subscribe(console.log); } + onSubmit(): void { + fetch('http://localhost:5000/api/User/register', { + method: 'POST', + body: `{ + "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' + } + }).then(response => response.json()).then(data => { console.log(data); }); + } + get firstName() { return this.registerUserFormGroup.get('firstName'); } @@ -71,4 +87,4 @@ export class RegisterComponent implements OnInit { get password() { return this.registerUserFormGroup.get('password'); } -} \ No newline at end of file +} -- cgit v1.2.3