Skip to content

Commit

Permalink
Tela de token invalido melhorado
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielVictorF committed Apr 20, 2019
1 parent ee91507 commit 6d8dda2
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 105 deletions.
33 changes: 27 additions & 6 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Platform, LoadingController, AlertController } from 'ionic-angular';

import { TabsPage } from '../pages/tabs/tabs';
import { LoginPage } from '../pages/login/login';
Expand All @@ -13,22 +13,43 @@ import { FunctionsProvider } from '../providers/functions/functions';
export class MyApp {
rootPage: any;

constructor(platform: Platform, public api: ApiProvider, public functions: FunctionsProvider) {
constructor(platform: Platform, public api: ApiProvider, public functions: FunctionsProvider,
public alertCtrl: AlertController, public loadingCtrl: LoadingController) {
platform.ready().then(() => {
if (localStorage.userToken) {
console.log(localStorage.userToken);
console.log("Tem token!");
this.api.validaToken().subscribe(res => {
console.log(res);
if (res) {
this.rootPage = TabsPage;
} else {
this.functions.logout();
this.rootPage = LoginPage;
this.logout();
}
});
} else {
this.rootPage = LoginPage;
}
});
}

logout() {
let load = this.alertCtrl.create({
title: 'Sessão expirou!',
message: 'Sua sessão expirou, por favor logue novamente.',
buttons: [{
text: 'OK',
handler: () => {
const load = this.loadingCtrl.create({
content: 'Saindo...'
});
load.present();
this.api.logout().subscribe(res => {
load.dismiss();
this.rootPage = LoginPage;
localStorage.removeItem("userToken");
});
}
}]
});
load.present();
}
}
15 changes: 14 additions & 1 deletion src/app/app.html
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
<ion-nav [root]="rootPage"></ion-nav>
<ion-menu [content]="menu">
<ion-header>
<ion-toolbar>
<ion-title>
Menu
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<menu></menu>
</ion-content>
</ion-menu>

<ion-nav [root]="rootPage" #menu></ion-nav>
5 changes: 4 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { DiaSemanaPipe } from '../pipes/dia-semana/dia-semana';
import { MesPipe } from '../pipes/mes/mes';
import { HorarioPipe } from '../pipes/horario/horario';

import { MenuComponent } from '../components/menu/menu';

@NgModule({
declarations: [
MyApp,
Expand All @@ -45,7 +47,8 @@ import { HorarioPipe } from '../pipes/horario/horario';
DataPipe,
DiaSemanaPipe,
MesPipe,
HorarioPipe
HorarioPipe,
MenuComponent
],
imports: [
BrowserModule,
Expand Down
8 changes: 8 additions & 0 deletions src/components/components.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NgModule } from '@angular/core';
import { MenuComponent } from './menu/menu';
@NgModule({
declarations: [MenuComponent],
imports: [],
exports: [MenuComponent]
})
export class ComponentsModule {}
2 changes: 2 additions & 0 deletions src/components/menu/menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<ion-item>Meu Perfil</ion-item>
<ion-item (click)="logout()">Sair</ion-item>
3 changes: 3 additions & 0 deletions src/components/menu/menu.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
menu {

}
56 changes: 56 additions & 0 deletions src/components/menu/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Component } from '@angular/core';

import { AlertController, LoadingController, MenuController, App } from 'ionic-angular';

import { LoginPage } from '../../pages/login/login';

import { ApiProvider } from '../../providers/api/api';

/**
* Generated class for the MenuComponent component.
*
* See https://angular.io/api/core/Component for more info on Angular
* Components.
*/
@Component({
selector: 'menu',
templateUrl: 'menu.html'
})
export class MenuComponent {

text: string;

constructor(public alertCtrl: AlertController, public loadingCtrl: LoadingController,
public app: App, public api: ApiProvider, public menuCtrl: MenuController) {
console.log('Hello MenuComponent Component');
this.text = 'Hello World';
}
logout() {
const confirm = this.alertCtrl.create({
title: 'Um momento',
message: 'Tem certeza que deseja sair?',
buttons: [{
text: 'Sim',
handler: () => {
const load = this.loadingCtrl.create({
content: 'Saindo...'
});
load.present();
this.api.logout().subscribe(res => {
load.dismiss();
this.menuCtrl.close();
localStorage.removeItem("userToken");
this.app.getRootNavs()[0].setRoot(LoginPage); //Erro de tabs solved
},
Error => {
console.log(Error);
});
}
},
{
text: 'Não'
}]
});
confirm.present();
}
}
7 changes: 4 additions & 3 deletions src/pages/adicionar/adicionar.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<ion-header>
<ion-navbar color="light">
<ion-buttons start>
<button ion-button icon-only (click)="logout()">
<ion-icon name="exit"></ion-icon>
<ion-buttons right>
<button ion-button icon-only menuToggle>
<ion-icon name='menu'></ion-icon>
</button>
</ion-buttons>

<ion-title text-center><span class="titulo">Registrar</span></ion-title>
</ion-navbar>
</ion-header>
Expand Down
29 changes: 0 additions & 29 deletions src/pages/adicionar/adicionar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,4 @@ import { DetalhePage } from '../detalhe/detalhe';
console.log(Error);
});
}

logout() {
const confirm = this.alertCtrl.create({
title: 'Um momento',
message: 'Tem certeza que deseja sair?',
buttons: [{
text: 'Sim',
handler: () => {
const load = this.loadingCtrl.create({
content: 'Saindo...'
});
load.present();
this.api.logout().subscribe(res => {
load.dismiss();
localStorage.removeItem("userToken");
this.navCtrl.setRoot(LoginPage);
},
Error => {
console.log(Error);
});
}
},
{
text: 'Não'
}]
});
confirm.present();
}
//}
}
6 changes: 3 additions & 3 deletions src/pages/home/home.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<ion-header>
<ion-navbar color="light">
<ion-buttons start>
<button ion-button icon-only (click)="logout()">
<ion-icon name="exit"></ion-icon>
<ion-buttons right>
<button ion-button icon-only menuToggle>
<ion-icon name='menu'></ion-icon>
</button>
</ion-buttons>
<ion-title text-center><span class="titulo">Home Glique</span></ion-title>
Expand Down
28 changes: 1 addition & 27 deletions src/pages/home/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,7 @@ export class HomePage {
this.navCtrl.push(MedicoesPage);
}

logout() {
const confirm = this.alertCtrl.create({
title: 'Um momento',
message: 'Tem certeza que deseja sair?',
buttons: [{
text: 'Sim',
handler: () => {
const load = this.loadingCtrl.create({
content: 'Saindo...'
});
load.present();
this.api.logout().subscribe(res => {
load.dismiss();
localStorage.removeItem("userToken");
this.app.getRootNavs()[0].setRoot(LoginPage); //Erro de tabs solved
},
Error => {
console.log(Error);
});
}
},
{
text: 'Não'
}]
});
confirm.present();
}


relatorios() {
this.navCtrl.push(RelatoriosPage);
Expand Down
40 changes: 5 additions & 35 deletions src/providers/functions/functions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ToastController, AlertController, LoadingController } from 'ionic-angular';
import { ToastController, AlertController, LoadingController, NavController } from 'ionic-angular';


import { LoginPage } from '../../pages/login/login';
import { ApiProvider } from '../api/api';

@Injectable()
export class FunctionsProvider {
private monthNames = ["Janeiro, Fevereiro, Março, Abril, Maio, Junho, Julho, Agosto, Setembro, Outubro, Novembro, Dezembro"];
Expand Down Expand Up @@ -115,40 +118,7 @@ export class FunctionsProvider {
});
alert.present();
}
logoutServidor() {
const url = this.REST_API + '/users/logout';
const httpOptions = ({
headers: new HttpHeaders({
'user-token': localStorage.userToken
})
});

return this.http.get(url, httpOptions);
}

logout() {
let load = this.alertCtrl.create({
title: 'Sessão expirou!',
message: 'Sua sessão expirou, por favor logue novamente.',
buttons: [{
text: 'OK',
handler: () => {
const load = this.loadingCtrl.create({
content: 'Saindo...'
});
load.present();
this.logoutServidor().subscribe(res => {
console.log("OK, deslogado");
load.dismiss();
localStorage.removeItem("userToken");
});
}
}]
});
load.present();
return load;
}


public formataData(tipo: number, data: Date) {
data = new Date(data);

Expand Down

0 comments on commit 6d8dda2

Please sign in to comment.