From 5ef59efe0791ef1274e4e5859d2f1dcf0ce3e6c1 Mon Sep 17 00:00:00 2001 From: DECSEF Sistemas Date: Mon, 10 Aug 2020 11:46:13 -0500 Subject: [PATCH] Changes to local system and enviroment distinction, --- src/app/loggin/loggin.page.ts | 48 +++++++++++++++++++++++++--- src/app/services/auth.service.ts | 8 ----- src/environments/environment.prod.ts | 3 +- src/environments/environment.ts | 3 +- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/app/loggin/loggin.page.ts b/src/app/loggin/loggin.page.ts index dc6d565..4734e6d 100644 --- a/src/app/loggin/loggin.page.ts +++ b/src/app/loggin/loggin.page.ts @@ -1,6 +1,8 @@ import { Component, OnInit, ViewChild } from '@angular/core'; import { NgForm } from '@angular/forms'; -import { NavController } from '@ionic/angular'; +import { NavController, LoadingController } from '@ionic/angular'; +import { AuthService } from '../services/auth.service'; +import { HttpErrorResponse } from '@angular/common/http'; @Component({ selector: 'app-loggin', @@ -11,7 +13,8 @@ export class LogginPage implements OnInit { @ViewChild(NgForm, {static: true}) ngForm: NgForm; errorMessage = null; data = {username: null, password: null,carrer:null}; - constructor( + constructor(private auth: AuthService, + private loading: LoadingController, private nav: NavController) { } ngOnInit() { @@ -41,7 +44,44 @@ export class LogginPage implements OnInit { this.errorMessage = 'Complete el formulario'; return; } - console.log(`Data:${this.data.carrer}`) - this.nav.navigateRoot('/tabs') + this.startLoading() + .then(_ => this.auth.signup(this.data).toPromise()) + .then(success => this.handleSuccess(success), error => this.handleError(error)) + .finally(() => this.stopoading()); + console.log(`Data:${this.data.carrer}`); + } + async startLoading(): Promise { + const loading = await this.loading.create({ + message: '', + translucent: true, + cssClass: 'custom-loading' + }); + return await loading.present(); + } + + async stopoading(): Promise { + this.loading.dismiss(); + } + + handleSuccess(response) { + this.errorMessage = null; + this.nav.navigateRoot('/tabs'); + } + + handleError(errorResponse: HttpErrorResponse) { + console.error(errorResponse.error); + + switch (errorResponse.error.message) { + case 'Bad credentials': + this.errorMessage = 'El usuario o la contraseña son inválidos'; + break; + default: + this.errorMessage = 'Intentelo de nuevo más tarde.'; + if (errorResponse.error.message) { + this.errorMessage = this.errorMessage + ` [${errorResponse.error.message}]`; + } else { + this.errorMessage = this.errorMessage + ` [${errorResponse.message}]`; + } + } } } diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 99c5692..846af6f 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -51,11 +51,6 @@ export class AuthService { .post(api('/user/sign-up'), signupPayoad) .pipe(tap(this.setSession.bind(this))); } - - getUserAlias(email: String): Observable{ - return this.http. - post(api('/user/alias-giver'),email); - } /** * Sets a user session with the given server auth response. * @param authResponse The Server response for authentication. @@ -64,8 +59,6 @@ export class AuthService { if (!authResponse.token || !authResponse.prefix) { return null; } - this.getUserAlias(authResponse.details.username) - .subscribe(data => { localStorage.setItem('alias',data.response);}); localStorage.setItem('token', authResponse.token); localStorage.setItem('username', authResponse.details.username); } @@ -73,7 +66,6 @@ export class AuthService { private clearSession(): void { localStorage.removeItem('token'); localStorage.removeItem('username'); - localStorage.removeItem('alias') localStorage.clear(); } } diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index 3612073..dfe53fb 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -1,3 +1,4 @@ export const environment = { - production: true + production: true, + server: 'https://132.2488.80.77:9707/' }; diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 7b4f817..840867b 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -3,7 +3,8 @@ // The list of file replacements can be found in `angular.json`. export const environment = { - production: false + production: false, + server: 'https://192.168.0.7:9707/' }; /*