소스 검색

autencticador de tokens locales ok, iniciado módulo de pagina inicial y productos

robert2206 8 년 전
부모
커밋
b43cd75530

+ 3 - 3
package.json

@@ -13,10 +13,10 @@
     "run:before": "build"
   },
   "dependencies": {
-    "ionic-angular": "^2.0.0-rc.0",
-    "ionicons": "^3.0.0",
     "@ionic/storage": "^1.0.3",
-    "ionic-native": "^2.0.3"
+    "ionic-angular": "^2.0.0-rc.0",
+    "ionic-native": "^2.0.3",
+    "ionicons": "^3.0.0"
   },
   "devDependencies": {
     "@ionic/app-scripts": "^0.0.23",

+ 13 - 29
src/app/app.component.ts

@@ -1,5 +1,5 @@
 import { Component, ViewChild } from '@angular/core';
-import { Nav, Platform } from 'ionic-angular';
+import { Nav, Platform, MenuController } from 'ionic-angular';
 import { StatusBar } from 'ionic-native';
 
 import { LoginPage } from '../pages/login/login';
@@ -7,37 +7,21 @@ import { Page1 } from '../pages/page1/page1';
 import { Page2 } from '../pages/page2/page2';
 
 @Component({
-  templateUrl: 'app.html'
+    templateUrl: 'app.html'
 })
 export class MyApp {
-  @ViewChild(Nav) nav: Nav;
+    @ViewChild(Nav) nav: Nav;
 
-  rootPage: any = LoginPage;
+    rootPage: any = LoginPage;
 
-  pages: Array<{title: string, component: any}>;
+    constructor(public platform: Platform, private menu: MenuController) {
+        this.initializeApp();
+        menu.enable(false);
+    }
 
-  constructor(public platform: Platform) {
-    this.initializeApp();
-
-    // used for an example of ngFor and navigation
-    this.pages = [
-      { title: 'Page One', component: Page1 },
-      { title: 'Page Two', component: Page2 }
-    ];
-
-  }
-
-  initializeApp() {
-    this.platform.ready().then(() => {
-      // Okay, so the platform is ready and our plugins are available.
-      // Here you can do any higher level native things you might need.
-      StatusBar.styleDefault();
-    });
-  }
-
-  openPage(page) {
-    // Reset the content nav to have just this page
-    // we wouldn't want the back button to show in this scenario
-    this.nav.setRoot(page.component);
-  }
+    initializeApp() {
+        this.platform.ready().then(() => {
+            StatusBar.styleDefault();
+        });
+    }
 }

+ 1 - 2
src/app/app.html

@@ -15,5 +15,4 @@
 
 </ion-menu>
 
-<!-- Disable swipe-to-go-back because it's poor UX to combine STGB with side menus -->
-<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
+<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>

+ 3 - 0
src/app/app.module.ts

@@ -2,6 +2,7 @@ import { NgModule } from '@angular/core';
 import { IonicApp, IonicModule } from 'ionic-angular';
 import { MyApp } from './app.component';
 import { LoginPage } from '../pages/login/login'
+import { HomePage } from '../pages/home/home'
 import { Page1 } from '../pages/page1/page1';
 import { Page2 } from '../pages/page2/page2';
 
@@ -9,6 +10,7 @@ import { Page2 } from '../pages/page2/page2';
   declarations: [
     MyApp,
     LoginPage,
+    HomePage,
     Page1,
     Page2
   ],
@@ -19,6 +21,7 @@ import { Page2 } from '../pages/page2/page2';
   entryComponents: [
     MyApp,
     LoginPage,
+    HomePage,
     Page1,
     Page2
   ],

+ 17 - 0
src/pages/home/home.html

@@ -0,0 +1,17 @@
+<ion-header>
+
+    <ion-navbar color="eiru">
+
+        <button ion-button menuToggle>
+          <ion-icon name="menu"></ion-icon>
+        </button>
+
+        <ion-title>Página principal</ion-title>
+
+    </ion-navbar>
+
+</ion-header>
+
+
+<ion-content padding>
+</ion-content>

+ 3 - 0
src/pages/home/home.scss

@@ -0,0 +1,3 @@
+page-home {
+
+}

+ 15 - 0
src/pages/home/home.ts

@@ -0,0 +1,15 @@
+import { Component } from '@angular/core';
+import { NavController } from 'ionic-angular';
+
+@Component({
+    selector: 'page-home',
+    templateUrl: 'home.html'
+})
+export class HomePage {
+
+    constructor(public navCtrl: NavController) {}
+
+    ionViewDidLoad() {
+        console.log('Hello Home Page');
+    }
+}

+ 1 - 1
src/pages/login/login.html

@@ -16,5 +16,5 @@
         </ion-item>
     </ion-list>
 
-    <button ion-button block color="primary" (click)="signin()" clearInput [disabled]="!url || !username || !password">Entrar</button>
+    <button ion-button block color="primary" (click)="signin()" [disabled]="!tokenService.url || !tokenService.username || !tokenService.password">Entrar</button>
 </ion-content>

+ 6 - 0
src/pages/login/login.ts

@@ -1,6 +1,7 @@
 import { Component } from '@angular/core';
 import { NavController } from 'ionic-angular';
 import { TokenService } from '../../providers/token.service';
+import { HomePage } from '../home/home'
 
 @Component({
     selector: 'page-login',
@@ -12,8 +13,13 @@ export class LoginPage {
 
     constructor(public navCtrl: NavController, public tokenService: TokenService) {}
 
+    ionViewWillEnter() {
+        console.log('Enter');
+    }
+
     ionViewDidLoad() {
         console.log('Hello Login Page');
+        this.navCtrl.setRoot(HomePage);
     }
 
     // Signin odoo

+ 5 - 0
src/pages/page1/page1.html

@@ -1,10 +1,15 @@
 <ion-header>
+
   <ion-navbar color="eiru">
+
     <button ion-button menuToggle>
       <ion-icon name="menu"></ion-icon>
     </button>
+
     <ion-title>Page One</ion-title>
+
   </ion-navbar>
+
 </ion-header>
 
 <ion-content padding>

+ 18 - 0
src/pages/product-list/product-list.html

@@ -0,0 +1,18 @@
+<!--
+  Generated template for the ProductList page.
+
+  See http://ionicframework.com/docs/v2/components/#navigation for more info on
+  Ionic pages and navigation.
+-->
+<ion-header>
+
+  <ion-navbar>
+    <ion-title>productList</ion-title>
+  </ion-navbar>
+
+</ion-header>
+
+
+<ion-content padding>
+
+</ion-content>

+ 3 - 0
src/pages/product-list/product-list.scss

@@ -0,0 +1,3 @@
+page-product-list {
+
+}

+ 22 - 0
src/pages/product-list/product-list.ts

@@ -0,0 +1,22 @@
+import { Component } from '@angular/core';
+import { NavController } from 'ionic-angular';
+
+/*
+  Generated class for the ProductList page.
+
+  See http://ionicframework.com/docs/v2/components/#navigation for more info on
+  Ionic pages and navigation.
+*/
+@Component({
+  selector: 'page-product-list',
+  templateUrl: 'product-list.html'
+})
+export class ProductList {
+
+  constructor(public navCtrl: NavController) {}
+
+  ionViewDidLoad() {
+    console.log('Hello ProductList Page');
+  }
+
+}

+ 47 - 13
src/providers/token.service.ts

@@ -15,41 +15,75 @@ export class TokenService {
     url: string = '192.168.100.141:22000';
 
     constructor(public http: Http) {
-        console.log('Hello TokenAuth Provider');
     }
 
+    // Get token
     getToken(): void {
-        let response = this.getTokenFromServer();
+        // let response = this.getTokenFromServer();
+        //
+        // response.subscribe(data => {
+        //         this.setLocalToken(data.value);
+        //     }, err => console.error(err),
+        //     () => console.log('Token received')
+        // );
+        this.checkTokenOnServer();
+    }
+
+    // Check if token is present locally
+    tokenIsPresent(): boolean {
+        return this.getTokenFromLocal() ? true : false;
+    }
+
+    // Check local token on the server
+    checkTokenOnServer(): Observable<Response> {
+        let body = 'token=' + this.getTokenFromLocal();
+        let headers = new Headers({ 'Content-Type':  'application/x-www-form-urlencoded' });
+        let options = new RequestOptions({ headers: headers });
 
-        response.subscribe(data => {
-                console.log(data.value);
-            }, err => console.error('Error: ' + err),
-            () => console.log('Token received')
-        );
+        return this.http.post(this.normalizeCheckUrl(this.url), body, options).map(this.extractCheckData).catch(this.handleError);
+    }
 
+    // Extract check value from token verification
+    private extractCheckData(response: Response): boolean {
+        let body = response.json();
+        return body.token == 'valid' ? true : false;
+    }
+
+    // Get token from
+    private getTokenFromLocal(): string {
+        return localStorage.getItem('odoo');
+    }
+
+    private setLocalToken(value): void {
+        localStorage.setItem('odoo', value);
     }
 
     // Check if introduced url is valid
-    checkUrl(): boolean {
+    private checkUrl(): boolean {
         return this.url.includes('.');
     }
 
     // Normalize provide url
-    private normalizeUrl(url: string): string {
+    private normalizeCheckUrl(url: string): string {
+        return (url.startsWith('http://') ? url : 'http://' + url) + '/api/check';
+    }
+
+    // Normalize provide url
+    private normalizeJwtUrl(url: string): string {
         return (url.startsWith('http://') ? url : 'http://' + url) + '/api/jwt';
     }
 
     // Get token from server
-    getTokenFromServer(): Observable<Token> {
+    private getTokenFromServer(): Observable<Token> {
         let body = 'username=' + this.username + '&password=' + this.password;
         let headers = new Headers({ 'Content-Type':  'application/x-www-form-urlencoded' });
         let options = new RequestOptions({ headers: headers });
 
-        return this.http.post(this.normalizeUrl(this.url), body, options).map(this.extractData).catch(this.handleError);
+        return this.http.post(this.normalizeJwtUrl(this.url), body, options).map(this.extractTokenData).catch(this.handleError);
     }
 
-    // Extract data from http response
-    private extractData(response: Response) {
+    // Extract token data from http response
+    private extractTokenData(response: Response) {
         let body = response.json();
         let token = new Token(body.token);