From d3eed6308a82510ab076e1b95c267e0b88d078f7 Mon Sep 17 00:00:00 2001 From: Chen-I Lim Date: Thu, 14 Jan 2021 09:34:08 -0800 Subject: [PATCH] Fix redirect to login if no session. Cleanup client error handling. --- webapp/src/app.tsx | 5 ++- webapp/src/octoClient.ts | 78 ++++++++++++++++++++++++---------------- webapp/src/user.tsx | 2 +- 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/webapp/src/app.tsx b/webapp/src/app.tsx index e01f895a1..3106ec9e3 100644 --- a/webapp/src/app.tsx +++ b/webapp/src/app.tsx @@ -23,7 +23,7 @@ import BoardPage from './pages/boardPage' type State = { language: string, - user: IUser|null, + user?: IUser initialLoad: boolean, } @@ -32,13 +32,12 @@ export default class App extends React.PureComponent { super(props) this.state = { language: getCurrentLanguage(), - user: null, initialLoad: false, } } public componentDidMount(): void { - client.getMe().then((user: IUser|null) => { + client.getMe().then((user?: IUser) => { this.setState({user, initialLoad: true}) }) } diff --git a/webapp/src/octoClient.ts b/webapp/src/octoClient.ts index 8284e8a55..1a9895827 100644 --- a/webapp/src/octoClient.ts +++ b/webapp/src/octoClient.ts @@ -32,15 +32,16 @@ class OctoClient { headers: this.headers(), body, }) - if (response.status === 200) { - const responseJson = (await response.json() || {}) as {token?: string} - this.token = responseJson.token - if (responseJson.token !== '') { - localStorage.setItem('sessionId', this.token || '') - return true - } + if (response.status !== 200) { return false } + + const responseJson = (await response.json() || {}) as {token?: string} + this.token = responseJson.token + if (responseJson.token !== '') { + localStorage.setItem('sessionId', this.token || '') + return true + } return false } @@ -66,10 +67,13 @@ class OctoClient { } } - async getMe(): Promise { + async getMe(): Promise { const path = '/api/v1/users/me' const response = await fetch(this.serverUrl + path, {headers: this.headers()}) - const user = (await response.json()) as IUser || null + if (response.status !== 200) { + return undefined + } + const user = (await response.json()) as IUser return user } @@ -79,6 +83,9 @@ class OctoClient { path += `&read_token=${this.readToken}` } const response = await fetch(this.serverUrl + path, {headers: this.headers()}) + if (response.status !== 200) { + return [] + } const blocks = (await response.json() || []) as IMutableBlock[] this.fixBlocks(blocks) return blocks @@ -87,6 +94,9 @@ class OctoClient { async exportFullArchive(): Promise { const path = '/api/v1/blocks/export' const response = await fetch(this.serverUrl + path, {headers: this.headers()}) + if (response.status !== 200) { + return [] + } const blocks = (await response.json() || []) as IMutableBlock[] this.fixBlocks(blocks) return blocks @@ -198,17 +208,19 @@ class OctoClient { }, body: formData, }) - if (response.status === 200) { - try { - const text = await response.text() - Utils.log(`uploadFile response: ${text}`) - const json = JSON.parse(text) + if (response.status !== 200) { + return undefined + } - // const json = await response.json() - return json.url - } catch (e) { - Utils.logError(`uploadFile json ERROR: ${e}`) - } + try { + const text = await response.text() + Utils.log(`uploadFile response: ${text}`) + const json = JSON.parse(text) + + // const json = await response.json() + return json.url + } catch (e) { + Utils.logError(`uploadFile json ERROR: ${e}`) } } catch (e) { Utils.logError(`uploadFile ERROR: ${e}`) @@ -219,10 +231,13 @@ class OctoClient { // Sharing - async getSharing(rootId: string): Promise { + async getSharing(rootId: string): Promise { const path = `/api/v1/sharing/${rootId}` const response = await fetch(this.serverUrl + path, {headers: this.headers()}) - const sharing = (await response.json()) as ISharing || null + if (response.status !== 200) { + return undefined + } + const sharing = (await response.json()) as ISharing return sharing } @@ -237,19 +252,22 @@ class OctoClient { body, }, ) - - if (response.status === 200) { - return true + if (response.status !== 200) { + return false } - return false + + return true } // Workspace - async getWorkspace(): Promise { + async getWorkspace(): Promise { const path = '/api/v1/workspace' const response = await fetch(this.serverUrl + path, {headers: this.headers()}) - const workspace = (await response.json()) as IWorkspace || null + if (response.status !== 200) { + return undefined + } + const workspace = (await response.json()) as IWorkspace return workspace } @@ -259,11 +277,11 @@ class OctoClient { method: 'POST', headers: this.headers(), }) - if (response.status === 200) { - return true + if (response.status !== 200) { + return false } - return false + return true } } diff --git a/webapp/src/user.tsx b/webapp/src/user.tsx index 4b80a1a2a..1c34a9991 100644 --- a/webapp/src/user.tsx +++ b/webapp/src/user.tsx @@ -3,7 +3,7 @@ import React from 'react' -const UserContext = React.createContext(null as IUser|null) +const UserContext = React.createContext(undefined as IUser|undefined) interface IUser { id: string,