From f1bf8ff75ebde1ecd9fbdea042034fdc270d5c89 Mon Sep 17 00:00:00 2001 From: onsaliyo Date: Mon, 8 Mar 2021 13:33:27 +0100 Subject: [PATCH] =?UTF-8?q?Montada=20comunicaci=C3=B3n=20con=20API,=20pero?= =?UTF-8?q?=20devuelve=20un=20objeto=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api.service.ts | 10 ++++++---- src/app/app-routing.module.ts | 2 +- src/app/login.service.ts | 28 +++++++++++++++++++++------- src/app/login/login.page.html | 7 +++++++ src/app/login/login.page.scss | 3 +++ src/app/login/login.page.ts | 18 +++++++++++++++--- src/app/user.ts | 6 +++--- 7 files changed, 56 insertions(+), 18 deletions(-) diff --git a/src/app/api.service.ts b/src/app/api.service.ts index b513eb1..b9bd55d 100644 --- a/src/app/api.service.ts +++ b/src/app/api.service.ts @@ -1,17 +1,18 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { Observable, throwError } from 'rxjs'; -import { catchError, retry } from 'rxjs/operators'; +import { Observable, throwError, BehaviorSubject } from 'rxjs'; +import { catchError, retry, map, tap } from 'rxjs/operators'; import { User } from './user'; import { Discoteca } from './discoteca'; import { UserLogin } from './user-login'; + @Injectable({ providedIn: 'root' }) export class ApiService { - url = ''; + constructor(private http: HttpClient) { @@ -20,8 +21,9 @@ export class ApiService { validateUser(user: UserLogin): Observable{ - return this.http.post(this.url, user); + return this.http.post("http://localhost:3307/api/consultas/users", user); } + } diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 7675048..c4f4dc7 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -23,7 +23,7 @@ const routes: Routes = [ }, { path:'', - redirectTo: 'tabs', + redirectTo: 'login', pathMatch: 'full' }, diff --git a/src/app/login.service.ts b/src/app/login.service.ts index f0fde60..beaf043 100644 --- a/src/app/login.service.ts +++ b/src/app/login.service.ts @@ -1,7 +1,9 @@ import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; import { ApiService } from './api.service'; import { User } from './user'; -import { UserLogin } from './user-login' +import { UserLogin } from './user-login'; + @Injectable({ providedIn: 'root' @@ -10,17 +12,29 @@ export class LoginService { user: User; - constructor(private apiService: ApiService) { - + constructor(private apiService: ApiService, private router: Router) { + this.user = { + id: 0, + userType: 0, + username: '', + password: '' + }; } - validateUser(login: string, password: string): void{ - let user : UserLogin = { + validateUser(login: string, password: string): User { + + let userlogin: UserLogin = { loginUser: login, loginPassword: password, } - this.apiService.validateUser(user) - .subscribe((data: User) => this.user = {...data}); + + this.apiService.validateUser(userlogin) + .subscribe(user => this.user = user) + + return this.user; + } + + } diff --git a/src/app/login/login.page.html b/src/app/login/login.page.html index 1a77f8b..cacbd4a 100644 --- a/src/app/login/login.page.html +++ b/src/app/login/login.page.html @@ -5,7 +5,14 @@ + Usuario: + ContraseƱa: Login +
+
+ {{user.username}} +
+
diff --git a/src/app/login/login.page.scss b/src/app/login/login.page.scss index e69de29..1bdc99e 100644 --- a/src/app/login/login.page.scss +++ b/src/app/login/login.page.scss @@ -0,0 +1,3 @@ +input{ + color: black; +} \ No newline at end of file diff --git a/src/app/login/login.page.ts b/src/app/login/login.page.ts index 75a025e..f8de934 100644 --- a/src/app/login/login.page.ts +++ b/src/app/login/login.page.ts @@ -1,5 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; +import { LoginService } from '../login.service'; +import { User } from '../user'; @Component({ selector: 'app-login', @@ -8,13 +10,23 @@ import { Router } from '@angular/router'; }) export class LoginPage implements OnInit { - constructor(private router: Router) { } + username: string; + password: string; + user: User; + + constructor(private router: Router, private loginService: LoginService) { } ngOnInit() { + this.user = this.loginService.user; } - login(){ - this.router.navigate(['/tabs']); + async login() { + + this.username = (document.getElementById("username")).value; + this.password = (document.getElementById("password")).value; + this.user = await this.loginService.validateUser(this.username, this.password); + console.log(this.user.username); + } } diff --git a/src/app/user.ts b/src/app/user.ts index 72434d2..5f9dd3f 100644 --- a/src/app/user.ts +++ b/src/app/user.ts @@ -1,6 +1,6 @@ export interface User { - userId: number; + id: number; userType: number; - loginUser: string; - loginPassword: string; + username: string; + password: string; }