Montada comunicación con API, pero devuelve un objeto undefined
This commit is contained in:
parent
659783d23f
commit
f1bf8ff75e
|
@ -1,17 +1,18 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import { Observable, throwError } from 'rxjs';
|
import { Observable, throwError, BehaviorSubject } from 'rxjs';
|
||||||
import { catchError, retry } from 'rxjs/operators';
|
import { catchError, retry, map, tap } from 'rxjs/operators';
|
||||||
import { User } from './user';
|
import { User } from './user';
|
||||||
import { Discoteca } from './discoteca';
|
import { Discoteca } from './discoteca';
|
||||||
import { UserLogin } from './user-login';
|
import { UserLogin } from './user-login';
|
||||||
|
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class ApiService {
|
export class ApiService {
|
||||||
|
|
||||||
url = '';
|
|
||||||
|
|
||||||
constructor(private http: HttpClient) {
|
constructor(private http: HttpClient) {
|
||||||
|
|
||||||
|
@ -20,8 +21,9 @@ export class ApiService {
|
||||||
|
|
||||||
validateUser(user: UserLogin): Observable<User>{
|
validateUser(user: UserLogin): Observable<User>{
|
||||||
|
|
||||||
return this.http.post<User>(this.url, user);
|
return this.http.post<User>("http://localhost:3307/api/consultas/users", user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ const routes: Routes = [
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path:'',
|
path:'',
|
||||||
redirectTo: 'tabs',
|
redirectTo: 'login',
|
||||||
pathMatch: 'full'
|
pathMatch: 'full'
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Router } from '@angular/router';
|
||||||
import { ApiService } from './api.service';
|
import { ApiService } from './api.service';
|
||||||
import { User } from './user';
|
import { User } from './user';
|
||||||
import { UserLogin } from './user-login'
|
import { UserLogin } from './user-login';
|
||||||
|
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
|
@ -10,17 +12,29 @@ export class LoginService {
|
||||||
|
|
||||||
user: User;
|
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{
|
validateUser(login: string, password: string): User {
|
||||||
let user : UserLogin = {
|
|
||||||
|
let userlogin: UserLogin = {
|
||||||
loginUser: login,
|
loginUser: login,
|
||||||
loginPassword: password,
|
loginPassword: password,
|
||||||
}
|
}
|
||||||
this.apiService.validateUser(user)
|
|
||||||
.subscribe((data: User) => this.user = {...data});
|
this.apiService.validateUser(userlogin)
|
||||||
|
.subscribe(user => this.user = user)
|
||||||
|
|
||||||
|
return this.user;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,14 @@
|
||||||
</ion-header>
|
</ion-header>
|
||||||
|
|
||||||
<ion-content>
|
<ion-content>
|
||||||
|
<span>Usuario: </span><input type="text" id="username">
|
||||||
|
<span>Contraseña: </span><input type="text" id="password">
|
||||||
<ion-button (click)="login()">
|
<ion-button (click)="login()">
|
||||||
Login
|
Login
|
||||||
</ion-button>
|
</ion-button>
|
||||||
|
<div>
|
||||||
|
<div *ngIf="user">
|
||||||
|
{{user.username}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</ion-content>
|
</ion-content>
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
input{
|
||||||
|
color: black;
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
|
import { LoginService } from '../login.service';
|
||||||
|
import { User } from '../user';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-login',
|
selector: 'app-login',
|
||||||
|
@ -8,13 +10,23 @@ import { Router } from '@angular/router';
|
||||||
})
|
})
|
||||||
export class LoginPage implements OnInit {
|
export class LoginPage implements OnInit {
|
||||||
|
|
||||||
constructor(private router: Router) { }
|
username: string;
|
||||||
|
password: string;
|
||||||
|
user: User;
|
||||||
|
|
||||||
|
constructor(private router: Router, private loginService: LoginService) { }
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
|
this.user = this.loginService.user;
|
||||||
}
|
}
|
||||||
|
|
||||||
login(){
|
async login() {
|
||||||
this.router.navigate(['/tabs']);
|
|
||||||
|
this.username = (<HTMLInputElement>document.getElementById("username")).value;
|
||||||
|
this.password = (<HTMLInputElement>document.getElementById("password")).value;
|
||||||
|
this.user = await this.loginService.validateUser(this.username, this.password);
|
||||||
|
console.log(this.user.username);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
export interface User {
|
export interface User {
|
||||||
userId: number;
|
id: number;
|
||||||
userType: number;
|
userType: number;
|
||||||
loginUser: string;
|
username: string;
|
||||||
loginPassword: string;
|
password: string;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue