From 2612c0853f0deb7c2d19cef3bf5a0b563ab16417 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 17 Jun 2017 07:57:10 -0700 Subject: [PATCH] added support for other platforms to authenticate, issue #9 --- src/app/app.component.ts | 29 +++++++++++++++-------- src/app/app.module.ts | 2 ++ src/pages/login/login.html | 20 +++++++++++++--- src/pages/login/login.ts | 48 ++++++++++++++++++++++++++++++-------- 4 files changed, 76 insertions(+), 23 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 55c5c49..a32904e 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -3,6 +3,7 @@ import { Nav, Platform } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import { Storage } from '@ionic/storage'; +import { Device } from '@ionic-native/device'; import { LoginPage } from '../pages/login/login'; import { StreamPage } from '../pages/stream/stream'; @@ -15,11 +16,12 @@ import * as pnut from 'pnut-butter'; export class MyApp { @ViewChild(Nav) nav: Nav; - rootPage: any = LoginPage; + rootPage: any = StreamPage; pages: Array<{title: string, component: any, params: Object}>; - constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, private storage: Storage) { + constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, + private storage: Storage, private device: Device) { this.initializeApp(); // used for an example of ngFor and navigation @@ -32,20 +34,27 @@ export class MyApp { initializeApp() { console.log('--- initializeApp ---'); - this.storage.get('token').then((val) => { - if (val.length > 1) { - pnut.token = val; - this.nav.setRoot(StreamPage, {stream: 'personal'}); - } - }).catch(err => { - console.log('ERROR: ' + err); - }); + 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. + + this.storage.get('token').then((val) => { + if (val.length > 1) { + pnut.token = val; + this.nav.setRoot(StreamPage, {stream: 'personal'}); + } + }).catch(err => { + console.log('ERROR: ' + err); + this.nav.setRoot(LoginPage); + }); + this.statusBar.styleDefault(); this.splashScreen.hide(); + console.log('---'); + console.log(this.device.platform); + console.log('---'); }); } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8b2fc8e..c99348a 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -10,6 +10,7 @@ import { ThreadPage } from '../pages/thread/thread'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import { IonicStorageModule } from '@ionic/storage'; +import { Device } from '@ionic-native/device'; import { TimeagoPipe } from '../pipes/timeago/timeago'; @NgModule({ @@ -37,6 +38,7 @@ import { TimeagoPipe } from '../pipes/timeago/timeago'; providers: [ StatusBar, SplashScreen, + Device, {provide: ErrorHandler, useClass: IonicErrorHandler} ] }) diff --git a/src/pages/login/login.html b/src/pages/login/login.html index f0cd07e..4a1d542 100644 --- a/src/pages/login/login.html +++ b/src/pages/login/login.html @@ -15,8 +15,22 @@ -

Goober

-

A mobile client for pnut.io

- +

 

+
+

Goober

+

A mobile client for pnut.io

+

 

+
+

Tap the Log In button to open browser window and enter your pnut.io creditionals and authorize Goober.

+
+

Afterwards, copy the token provided, close the pop up window, and paste the token into the input field shown and tap Save Token.

+
+

+

+
+

Paste the token provided into this field and then tap Save Token.

+ +

+
diff --git a/src/pages/login/login.ts b/src/pages/login/login.ts index 253b15d..7cb2c8b 100644 --- a/src/pages/login/login.ts +++ b/src/pages/login/login.ts @@ -1,10 +1,11 @@ import { Component } from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; import { Storage } from '@ionic/storage'; +import { Device } from '@ionic-native/device'; import { StreamPage } from '../stream/stream'; import { OauthCordova } from 'ng2-cordova-oauth/platform/cordova'; -// import { OauthBrowser } from 'ng2-cordova-oauth/platform/browser'; +import { OauthBrowser } from 'ng2-cordova-oauth/platform/browser'; import { PnutAuth } from '../../providers/pnut-oauth'; import * as pnut from 'pnut-butter'; @@ -20,13 +21,31 @@ import * as pnut from 'pnut-butter'; }) export class LoginPage { - private oauth: OauthCordova = new OauthCordova(); - // private oauthb: OauthBrowser = new OauthBrowser(); - private pnutProvider: PnutAuth = new PnutAuth({ - appScope: ['basic','stream','write_post'] - }); + private oauth: any; + private pnutProvider: any; + private oob: boolean = false; + private showToken: boolean = false; + private token: string; - constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage) { + constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage, + private device: Device) { + + let scope = ['basic','stream','write_post']; + + if (this.device.platform === "Android") { + this.oauth = new OauthCordova(); + this.pnutProvider = new PnutAuth({ + appScope: scope, + redirectUri: 'http://localhost/callback' + }); + } else { + this.oauth = new OauthBrowser(); + this.pnutProvider = new PnutAuth({ + appScope: scope, + redirectUri: 'urn:ietf:wg:oauth:2.0:oob' + }); + this.oob = true; + } } @@ -35,10 +54,19 @@ export class LoginPage { console.log('RESULT: ' + JSON.stringify(success)); this.storage.set('token', success['access_token']); pnut.token = success['access_token']; - this.navCtrl.setRoot(StreamPage, {stream: 'global'}); - }, error=> { - console.log('ERROR: ' + error); + this.navCtrl.setRoot(StreamPage, {stream: 'personal'}); + }, error => { + console.log(error); + if (this.oob) { + this.showToken = true; + } }); } + saveToken() { + this.storage.set('token', this.token); + pnut.token = this.token; + this.navCtrl.setRoot(StreamPage, {stream: 'personal'}); + } + }