فهرست منبع

recuperar token del servidor

robert2206 8 سال پیش
والد
کامیت
2b9cde4322

+ 2 - 2
src/app/app.component.ts

@@ -2,7 +2,7 @@ import { Component, ViewChild } from '@angular/core';
 import { Nav, Platform } from 'ionic-angular';
 import { StatusBar } from 'ionic-native';
 
-import { Login } from '../pages/login/login';
+import { LoginPage } from '../pages/login/login';
 import { Page1 } from '../pages/page1/page1';
 import { Page2 } from '../pages/page2/page2';
 
@@ -12,7 +12,7 @@ import { Page2 } from '../pages/page2/page2';
 export class MyApp {
   @ViewChild(Nav) nav: Nav;
 
-  rootPage: any = Login;
+  rootPage: any = LoginPage;
 
   pages: Array<{title: string, component: any}>;
 

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

@@ -1,15 +1,14 @@
 import { NgModule } from '@angular/core';
 import { IonicApp, IonicModule } from 'ionic-angular';
 import { MyApp } from './app.component';
-import { Login } from '../pages/login/login'
-import { TokenAuth } from '../providers/token-auth';
+import { LoginPage } from '../pages/login/login'
 import { Page1 } from '../pages/page1/page1';
 import { Page2 } from '../pages/page2/page2';
 
 @NgModule({
   declarations: [
     MyApp,
-    Login,
+    LoginPage,
     Page1,
     Page2
   ],
@@ -19,8 +18,7 @@ import { Page2 } from '../pages/page2/page2';
   bootstrap: [IonicApp],
   entryComponents: [
     MyApp,
-    Login,
-    TokenAuth,
+    LoginPage,
     Page1,
     Page2
   ],

+ 4 - 0
src/components/login.component.ts

@@ -0,0 +1,4 @@
+export class Login {
+
+    constructor(public url: string, public username: string, public password: string) {}
+}

+ 4 - 0
src/components/token.component.ts

@@ -0,0 +1,4 @@
+export class Token {
+
+    constructor(public value: string) {}
+}

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

@@ -1,19 +1,20 @@
 import { Component } from '@angular/core';
 import { NavController } from 'ionic-angular';
-import { TokenAuth } from '../../providers/token-auth';
+import { TokenService } from '../../providers/token.service';
 
 @Component({
   selector: 'page-login',
+  providers: [TokenService],
   templateUrl: 'login.html'
 })
 
-export class Login {
+export class LoginPage {
 
-    url: string;
-    username: string;
-    password: string;
+    url: string = '192.168.100.141';
+    username: string = 'admin';
+    password: string = 'admin';
 
-    constructor(public navCtrl: NavController) {}
+    constructor(public navCtrl: NavController, public tokenService: TokenService) {}
 
     ionViewDidLoad() {
         console.log('Hello Login Page');
@@ -31,5 +32,9 @@ export class Login {
         }
 
         console.log(this.url);
+
+        // let result = this.tokenService.getTokenFromServer(this.url, this.username, this.password);
+
+        // console.log(result);
     }
 }

+ 0 - 45
src/providers/token-auth.ts

@@ -1,45 +0,0 @@
-import { Injectable } from '@angular/core';
-import { Http, Response } from '@angular/http';
-import 'rxjs/add/operator/toPromise';
-import 'rxjs/add/operator/catch';
-
-@Injectable()
-export class TokenAuth {
-
-    token: string;
-
-    constructor(public http: Http) {
-        console.log('Hello TokenAuth Provider');
-    }
-
-    getToken() {
-
-    }
-
-    // Check if token exist on mobile device
-    private tokenExists(): boolean {
-        return true;
-    }
-
-    // Get token from server
-    private getTokenFromServer(url: string, username: string, password: string): Promise<string> {
-        let body = JSON.stringify({
-            username: username,
-            password: password
-        });
-
-        return this.http.post(url, body).toPromise().then(this.extractData).catch(this.handleError);
-    }
-
-    // Extract data from http response
-    private extractData(response: Response) {
-        let body = response.json();
-
-        return body.data || {};
-    }
-
-    // Handle error if ocurred
-    private handleError(error: any) {
-        return Promise.reject(error.response);
-    }
-}

+ 46 - 0
src/providers/token.service.ts

@@ -0,0 +1,46 @@
+import { Injectable } from '@angular/core';
+import { Http, Response, Headers, RequestOptions } from '@angular/http';
+import { Observable } from 'rxjs/Observable'
+import { Token } from '../components/token.component';
+
+import 'rxjs/add/operator/map';
+import 'rxjs/add/operator/catch';
+import 'rxjs/add/observable/throw'
+
+@Injectable()
+export class TokenService {
+
+    constructor(public http: Http) {
+        console.log('Hello TokenAuth Provider');
+    }
+
+    // Get token from server
+    getTokenFromServer(url: string, username: string, password: string): Observable<Token> {
+        let body = JSON.stringify({
+            username: username,
+            password: password
+        });
+
+        let headers = new Headers({ 'Content-Type':  'multipart/form-data'});
+        let options = new RequestOptions({ headers: headers });
+
+        return this.http.post(this.normalizeUrl(url), body, options).map(this.extractData).catch(this.handleError);
+    }
+
+    // Normalize provide url
+    private normalizeUrl(url: string): string {
+        return (url.startsWith('http://') ? url : 'http://' + url) + '/jwt/api';
+    }
+
+    // Extract data from http response
+    private extractData(response: Response) {
+        let body = response.json();
+
+        return body.data || {};
+    }
+
+    // Handle error if ocurred
+    private handleError(error: any) {
+        return Observable.throw(error.response);
+    }
+}