From e55fafe04bfe7439601e6ca83ecf00304f640d60 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Mon, 19 Feb 2018 18:35:21 -0800 Subject: [PATCH] handle invalid auth token resolve #52 --- src/pages/stream/stream.ts | 49 +++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/pages/stream/stream.ts b/src/pages/stream/stream.ts index 7415dc8..595b9c6 100644 --- a/src/pages/stream/stream.ts +++ b/src/pages/stream/stream.ts @@ -6,6 +6,7 @@ import { FileChooser } from '@ionic-native/file-chooser'; import { FilePath } from '@ionic-native/file-path'; import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer'; import { Events } from 'ionic-angular'; +import { LoginPage } from '../login/login'; import * as pnut from 'pnut-butter'; @@ -166,13 +167,19 @@ export class StreamPage { count: 40 }; this.fetcher(params).then(res => { - this.posts = this.parseData(res.data); - this.since_id = res.meta.max_id; - this.before_id = res.meta.min_id; - console.log('since_id: ' + this.since_id); - console.log('before_id: ' + this.before_id); - }).catch(err => { - console.log(err); + console.log('-fetch success-'); + console.log(JSON.stringify(res)); + if (res.meta.code === 401) { + this.storage.clear(); + this.navCtrl.setRoot(LoginPage); + } else { + this.posts = this.parseData(res.data); + this.since_id = res.meta.max_id; + this.before_id = res.meta.min_id; + console.log('since_id: ' + this.since_id); + console.log('before_id: ' + this.before_id); + } + }); } @@ -204,22 +211,28 @@ export class StreamPage { fetchThread(threadid) { pnut.thread(threadid, {include_deleted: 0, include_raw: 1, count: 140}).then(res => { - this.navCtrl.push(ThreadPage, {posts: res.data, me: this.myUsername}); - }).catch(err => { - console.log(err); + if (res.meta.code === 401) { + this.storage.clear(); + this.navCtrl.setRoot(LoginPage); + } else { + this.navCtrl.push(ThreadPage, {posts: res.data, me: this.myUsername}); + } }); } fetchMyPosts() { console.log('-- fetching mentions --'); this.fetcher('me', {include_raw: 1, count: 40}).then(res => { - this.posts = res.data; - this.since_id = res.meta.max_id; - this.before_id = res.meta.min_id; - console.log('since_id: ' + this.since_id); - console.log('before_id: ' + this.before_id); - }).catch(err => { - console.log(err); + if (res.meta.code === 401) { + this.storage.clear(); + this.navCtrl.setRoot(LoginPage); + } else { + this.posts = res.data; + this.since_id = res.meta.max_id; + this.before_id = res.meta.min_id; + console.log('since_id: ' + this.since_id); + console.log('before_id: ' + this.before_id); + } }); } @@ -529,6 +542,8 @@ export class NewPostModal { console.log('-create file error-'); console.log(JSON.stringify(err)); console.log('-create file error-'); + let edata = JSON.parse(err.body); + this.presentToast(edata.meta.error_message); }); }).catch(err => {