From 4f8768966c360ce935f02d3a4d6fa6e1f86385bb Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 18 May 2019 16:23:00 -0700 Subject: [PATCH] replace login --- src/app/app.component.ts | 12 ++--- src/app/app.module.ts | 2 + src/pages/login/login.html | 19 +++----- src/pages/login/login.ts | 91 ++++++++++++++------------------------ 4 files changed, 47 insertions(+), 77 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index ff685d0..3af14ce 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -9,6 +9,7 @@ import { StreamPage } from '../pages/stream/stream'; import { SettingsPage } from '../pages/settings/settings'; import { AboutPage } from '../pages/about/about'; import { ProfilePage } from '../pages/profile/profile'; +import { pnutauth } from '../pages/login/pnutauth'; import { IUser } from '../models/IUser'; @@ -22,7 +23,6 @@ export class MyApp { rootPage: any = StreamPage; pages: Array<{title: string, icon: string, component: any, params: Object}>; - scope: Array = ['basic','stream','write_post','files']; profile: IUser; constructor(public platform: Platform, public splashScreen: SplashScreen, @@ -73,20 +73,20 @@ export class MyApp { pnut.token = val; this.storage.get('scope').then((sval) => { - if (JSON.stringify(sval) !== JSON.stringify(this.scope)) { - this.nav.setRoot(LoginPage, {'scope': this.scope}); + if (JSON.stringify(sval) !== JSON.stringify(pnutauth.scope)) { + this.nav.setRoot(LoginPage, {}); } else { this.nav.setRoot(StreamPage, {stream: timeline}); } // this.nav.setRoot(StreamPage, {stream: timeline}); }).catch(err => { - this.nav.setRoot(LoginPage, {'scope': this.scope}); + this.nav.setRoot(LoginPage, {}); }); // this.nav.setRoot(StreamPage, {stream: timeline}); } }).catch(err => { console.log('ERROR: ' + err); - this.nav.setRoot(LoginPage, {'scope': this.scope}); + this.nav.setRoot(LoginPage, {}); }); } @@ -96,7 +96,7 @@ export class MyApp { if (page.title === 'Logout') { // this.storage.remove('token'); this.storage.clear(); - this.nav.setRoot(LoginPage, {'scope': this.scope}); + this.nav.setRoot(LoginPage, {}); } else if (page.title === 'Settings' || page.title === 'About') { this.nav.push(page.component, page.params); } else if (page.title === 'Profile') { diff --git a/src/app/app.module.ts b/src/app/app.module.ts index b5dc8f1..57781eb 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,6 +1,7 @@ import { BrowserModule } from '@angular/platform-browser'; import { ErrorHandler, NgModule } from '@angular/core'; import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; +import { HttpClientModule } from '@angular/common/http'; import { MyApp } from './app.component'; import { LoginPage } from '../pages/login/login'; @@ -44,6 +45,7 @@ import { ClipboardModule } from 'ngx-clipboard'; ], imports: [ BrowserModule, + HttpClientModule, IonicModule.forRoot(MyApp), IonicStorageModule.forRoot(), ClipboardModule, diff --git a/src/pages/login/login.html b/src/pages/login/login.html index 4a1d542..b0f6a36 100644 --- a/src/pages/login/login.html +++ b/src/pages/login/login.html @@ -20,17 +20,12 @@

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.

- -

-
+ +Username + +Password + + + diff --git a/src/pages/login/login.ts b/src/pages/login/login.ts index 831f709..d6c2ef8 100644 --- a/src/pages/login/login.ts +++ b/src/pages/login/login.ts @@ -1,81 +1,54 @@ import { Component } from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; +import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http'; import { Storage } from '@ionic/storage'; -import { Device } from '@ionic-native/device'; import { StreamPage } from '../stream/stream'; +import { pnutauth } from './pnutauth'; -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. - */ + @Component({ selector: 'page-login', templateUrl: 'login.html', }) export class LoginPage { - private oauth: any; - private pnutProvider: any; - private oob: boolean = false; - public showToken: boolean = false; - private token: string; - private scope: Array = []; + private username: string; + private password: string; - constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage, - private device: Device) { - - this.scope = navParams.data.scope; - - if (this.device.platform === "Android" || this.device.platform === "amazon-fireos") { - this.oauth = new OauthCordova(); - this.pnutProvider = new PnutAuth({ - appScope: this.scope, - redirectUri: 'http://localhost/callback' - }); - } else if (this.device.platform === "blackberry10") { - this.oauth = new OauthBrowser(); - this.pnutProvider = new PnutAuth({ - appScope: this.scope, - redirectUri: 'https://zoidberg.monkeystew.net/' - }); - } else { - this.oauth = new OauthBrowser(); - this.pnutProvider = new PnutAuth({ - appScope: this.scope, - redirectUri: 'urn:ietf:wg:oauth:2.0:oob' - }); - this.oob = true; - } + constructor(public navCtrl: NavController, public navParams: NavParams, + private storage: Storage, private http: HttpClient) { } login() { - this.oauth.logInVia(this.pnutProvider).then(success => { - // console.log('OAUTH SUCCESS: ' + JSON.stringify(success)); - this.storage.set('token', success['access_token']); - this.storage.set('scope', this.scope); - pnut.token = success['access_token']; - this.navCtrl.setRoot(StreamPage, {stream: 'personal'}); - }, error => { - // console.log('OAUTH ERROR: ' + JSON.stringify(error)); - if (this.oob) { - this.showToken = true; - } - }); - } - saveToken() { - this.storage.set('scope', this.scope); - this.storage.set('token', this.token); - pnut.token = this.token; - this.navCtrl.setRoot(StreamPage, {stream: 'personal'}); + interface LoginResponse { + access_token: string; + } + + let headers = new HttpHeaders() + .set('Content-Type', 'application/x-www-form-urlencoded'); + + let params = new HttpParams() + .set('client_id', pnutauth.clientId) + .set('password_grant_secret', pnutauth.clientSecret) + .set('username', this.username) + .set('password', this.password) + .set('grant_type', 'password') + .set('scope', pnutauth.scope); + + this.http.post(pnutauth.url, params, {headers: headers}).subscribe(res => { + console.log('authorized'); + this.storage.set('scope', pnutauth.scope); + this.storage.set('token', res.access_token); + pnut.token = res.access_token; + this.navCtrl.setRoot(StreamPage, {stream: "personal"}); + }, err => { + console.log("error: " + JSON.stringify(err)); + }); + } }