From d395504629a65bb242e7d197fe70d58277f9a0d1 Mon Sep 17 00:00:00 2001
From: Morgan McMillian
Date: Tue, 6 Jun 2017 16:42:08 -0700
Subject: [PATCH] added oauth
---
.gitignore | 3 ++
README.md | 8 +++---
config.xml | 3 +-
package.json | 6 +++-
src/app/app.component.ts | 20 +++++++++++--
src/app/app.module.ts | 9 ++++--
src/pages/home/home.html | 1 +
src/pages/home/home.ts | 23 ++++++++++++++-
src/pages/list/list.ts | 8 ++++++
src/pages/login/login.html | 22 +++++++++++++++
src/pages/login/login.module.ts | 16 +++++++++++
src/pages/login/login.scss | 3 ++
src/pages/login/login.ts | 45 ++++++++++++++++++++++++++++++
src/providers/pnut-oauth.ts.sample | 11 ++++++++
14 files changed, 166 insertions(+), 12 deletions(-)
create mode 100644 src/pages/login/login.html
create mode 100644 src/pages/login/login.module.ts
create mode 100644 src/pages/login/login.scss
create mode 100644 src/pages/login/login.ts
create mode 100644 src/providers/pnut-oauth.ts.sample
diff --git a/.gitignore b/.gitignore
index c558400..955e6da 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,3 +32,6 @@ $RECYCLE.BIN/
.DS_Store
Thumbs.db
UserInterfaceState.xcuserstate
+
+# other bits
+pnut-oauth.ts
diff --git a/README.md b/README.md
index fea72f2..9424630 100644
--- a/README.md
+++ b/README.md
@@ -24,7 +24,7 @@ limitations under the License.
* Install Node.js (https://nodejs.org)
* Install the latest Ionic and Cordova tools
- ```
+ ```bash
npm install -g ionic cordova
```
@@ -47,7 +47,7 @@ limitations under the License.
## Other build dependencies
Install required node modules
-```
+```bash
cd Goober
npm install
```
@@ -56,12 +56,12 @@ npm install
## Build and run
#### Browser
-```
+```bash
ionic serve --lab
```
#### Android
-```
+```bash
ionic cordova platform add android # if not yet added
ionic cordova build android
ionic cordova run android
diff --git a/config.xml b/config.xml
index 097146c..919eb4c 100644
--- a/config.xml
+++ b/config.xml
@@ -1,5 +1,5 @@
-
+
Goober
Goober, a mobile app for pnut.io
Morgan McMillian
@@ -82,6 +82,7 @@
+
diff --git a/package.json b/package.json
index ff810d9..f560c08 100644
--- a/package.json
+++ b/package.json
@@ -28,6 +28,7 @@
"cordova-blackberry10": "^3.8.0",
"cordova-plugin-console": "^1.0.5",
"cordova-plugin-device": "^1.1.4",
+ "cordova-plugin-inappbrowser": "^1.7.1",
"cordova-plugin-splashscreen": "^4.0.3",
"cordova-plugin-statusbar": "^2.2.2",
"cordova-plugin-whitelist": "^1.3.1",
@@ -35,6 +36,8 @@
"ionic-angular": "3.3.0",
"ionic-plugin-keyboard": "^2.2.1",
"ionicons": "3.0.0",
+ "ng2-cordova-oauth": "0.0.8",
+ "pnut-butter": "^0.8.0",
"rxjs": "5.1.1",
"sw-toolbox": "3.6.0",
"zone.js": "0.8.11"
@@ -53,7 +56,8 @@
"cordova-plugin-splashscreen": {},
"cordova-plugin-statusbar": {},
"cordova-plugin-whitelist": {},
- "ionic-plugin-keyboard": {}
+ "ionic-plugin-keyboard": {},
+ "cordova-plugin-inappbrowser": {}
},
"platforms": [
"android",
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 9adbbfc..7966a6b 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -2,9 +2,12 @@ import { Component, ViewChild } from '@angular/core';
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 { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
+import { LoginPage } from '../pages/login/login';
+import * as pnut from 'pnut-butter';
@Component({
templateUrl: 'app.html'
@@ -12,22 +15,32 @@ import { ListPage } from '../pages/list/list';
export class MyApp {
@ViewChild(Nav) nav: Nav;
- rootPage: any = HomePage;
+ rootPage: any = LoginPage;
pages: Array<{title: string, component: any}>;
- constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen) {
+ constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, private storage: Storage) {
this.initializeApp();
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Home', component: HomePage },
- { title: 'List', component: ListPage }
+ { title: 'List', component: ListPage },
];
}
initializeApp() {
+ console.log('--- initializeApp ---');
+ this.storage.get('token').then((val) => {
+ if (val.length > 1) {
+ pnut.token = val;
+ this.nav.setRoot(HomePage);
+ }
+ }).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.
@@ -41,4 +54,5 @@ export class MyApp {
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
+
}
diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index c61a9c2..6787d41 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -5,25 +5,30 @@ import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
+import { LoginPage } from '../pages/login/login';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
+import { IonicStorageModule } from '@ionic/storage';
@NgModule({
declarations: [
MyApp,
HomePage,
- ListPage
+ ListPage,
+ LoginPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
+ IonicStorageModule.forRoot(),
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
- ListPage
+ ListPage,
+ LoginPage
],
providers: [
StatusBar,
diff --git a/src/pages/home/home.html b/src/pages/home/home.html
index 50de7f2..d3efca1 100644
--- a/src/pages/home/home.html
+++ b/src/pages/home/home.html
@@ -15,4 +15,5 @@
+
diff --git a/src/pages/home/home.ts b/src/pages/home/home.ts
index 1d510dd..20f113d 100644
--- a/src/pages/home/home.ts
+++ b/src/pages/home/home.ts
@@ -1,5 +1,8 @@
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
+import { Storage } from '@ionic/storage';
+
+import * as pnut from 'pnut-butter';
@Component({
selector: 'page-home',
@@ -7,8 +10,26 @@ import { NavController } from 'ionic-angular';
})
export class HomePage {
- constructor(public navCtrl: NavController) {
+ constructor(public navCtrl: NavController, private storage: Storage) {
+ console.log('*** Created HomePage ***');
+ this.fetchMe();
+ }
+ fetchGlobal() {
+ console.log('.... going to fetch global maybe ....');
+ pnut.global().then(res => {
+ console.log(res);
+ }).catch(err => {
+ console.log(err);
+ });
+ }
+
+ fetchMe() {
+ pnut.user('me').then(res => {
+ console.log('RESULT: ' + JSON.stringify(res));
+ }).catch( err => {
+ console.log('ERROR: ' + JSON.stringify(err));
+ });
}
}
diff --git a/src/pages/list/list.ts b/src/pages/list/list.ts
index a421ca9..8719a95 100644
--- a/src/pages/list/list.ts
+++ b/src/pages/list/list.ts
@@ -1,6 +1,8 @@
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
+import * as pnut from 'pnut-butter';
+
@Component({
selector: 'page-list',
templateUrl: 'list.html'
@@ -11,6 +13,12 @@ export class ListPage {
items: Array<{title: string, note: string, icon: string}>;
constructor(public navCtrl: NavController, public navParams: NavParams) {
+ console.log('*** created ListPage ***');
+ pnut.user('me').then(res => {
+ console.log('RESULT: ' + JSON.stringify(res));
+ }).catch( err => {
+ console.log('ERROR: ' + JSON.stringify(err));
+ });
// If we navigated to this page, we will have an item available as a nav param
this.selectedItem = navParams.get('item');
diff --git a/src/pages/login/login.html b/src/pages/login/login.html
new file mode 100644
index 0000000..f0cd07e
--- /dev/null
+++ b/src/pages/login/login.html
@@ -0,0 +1,22 @@
+
+
+
+
+ Login to pnut.io
+
+
+
+
+
+
+
+Goober
+A mobile client for pnut.io
+
+
+
diff --git a/src/pages/login/login.module.ts b/src/pages/login/login.module.ts
new file mode 100644
index 0000000..1fc75b4
--- /dev/null
+++ b/src/pages/login/login.module.ts
@@ -0,0 +1,16 @@
+import { NgModule } from '@angular/core';
+import { IonicPageModule } from 'ionic-angular';
+import { LoginPage } from './login';
+
+@NgModule({
+ declarations: [
+ LoginPage,
+ ],
+ imports: [
+ IonicPageModule.forChild(LoginPage),
+ ],
+ exports: [
+ LoginPage
+ ]
+})
+export class LoginPageModule {}
diff --git a/src/pages/login/login.scss b/src/pages/login/login.scss
new file mode 100644
index 0000000..2b94c14
--- /dev/null
+++ b/src/pages/login/login.scss
@@ -0,0 +1,3 @@
+page-login {
+
+}
diff --git a/src/pages/login/login.ts b/src/pages/login/login.ts
new file mode 100644
index 0000000..e6d942b
--- /dev/null
+++ b/src/pages/login/login.ts
@@ -0,0 +1,45 @@
+import { Component } from '@angular/core';
+import { IonicPage, NavController, NavParams } from 'ionic-angular';
+import { Storage } from '@ionic/storage';
+import { HomePage } from '../home/home';
+
+import { OauthCordova } from 'ng2-cordova-oauth/platform/cordova';
+import { OauthBrowser } from 'ng2-cordova-oauth/platform/browser';
+import { PnutAuth } from '../../providers/pnut-oauth';
+import * as pnut from 'pnut-butter';
+
+/**
+ * Generated class for the LoginPage page.
+ *
+ * See http://ionicframework.com/docs/components/#navigation for more info
+ * on Ionic pages and navigation.
+ */
+@IonicPage()
+@Component({
+ selector: 'page-login',
+ templateUrl: 'login.html',
+})
+export class LoginPage {
+
+ private oauth: OauthCordova = new OauthCordova();
+ private oauthb: OauthBrowser = new OauthBrowser();
+ private pnutProvider: PnutAuth = new PnutAuth({
+ appScope: ['basic','stream','write_post']
+ });
+
+ constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage) {
+
+ }
+
+ login() {
+ this.oauthb.logInVia(this.pnutProvider).then(success => {
+ // console.log('RESULT: ' + JSON.stringify(success));
+ this.storage.set('token', success['access_token']);
+ pnut.token = success['access_token'];
+ this.navCtrl.setRoot(HomePage);
+ }, error=> {
+ console.log('ERROR: ' + error);
+ });
+ }
+
+}
diff --git a/src/providers/pnut-oauth.ts.sample b/src/providers/pnut-oauth.ts.sample
new file mode 100644
index 0000000..996bf00
--- /dev/null
+++ b/src/providers/pnut-oauth.ts.sample
@@ -0,0 +1,11 @@
+import { OAuthProvider } from 'ng2-cordova-oauth/provider';
+
+export class PnutAuth extends OAuthProvider {
+
+ protected authUrl: string = 'https://pnut.io/oauth/authenticate';
+ protected defaults: Object = {
+ responseType: 'token',
+ clientId: '' // Insert your client ID and rename this file to pnut-oauth.ts
+ };
+
+}