diff --git a/server/app/auth.go b/server/app/auth.go index 18f644d12..45779bda0 100644 --- a/server/app/auth.go +++ b/server/app/auth.go @@ -94,7 +94,7 @@ func (a *App) RegisterUser(username string, email string, password string) error var err error user, err = a.store.GetUserByUsername(username) if err == nil && user != nil { - return errors.Wrap(err, "The username already exists") + return errors.New("The username already exists") } } @@ -102,7 +102,7 @@ func (a *App) RegisterUser(username string, email string, password string) error var err error user, err = a.store.GetUserByEmail(email) if err == nil && user != nil { - return errors.Wrap(err, "The email already exists") + return errors.New("The email already exists") } } diff --git a/webapp/src/app.tsx b/webapp/src/app.tsx index 3106ec9e3..4a26092eb 100644 --- a/webapp/src/app.tsx +++ b/webapp/src/app.tsx @@ -65,11 +65,11 @@ export default class App extends React.PureComponent { - + {this.state.initialLoad && !this.state.user && } - + {this.state.initialLoad && !this.state.user && } diff --git a/webapp/src/octoClient.ts b/webapp/src/octoClient.ts index 1a9895827..3e4a79227 100644 --- a/webapp/src/octoClient.ts +++ b/webapp/src/octoClient.ts @@ -24,6 +24,15 @@ class OctoClient { Utils.log(`OctoClient serverUrl: ${this.serverUrl}`) } + private async getJson(response: Response, defaultValue: any = {}): Promise { + // The server may return null or malformed json + try { + return await response.json() + } catch { + return defaultValue + } + } + async login(username: string, password: string): Promise { const path = '/api/v1/login' const body = JSON.stringify({username, password, type: 'normal'}) @@ -36,7 +45,7 @@ class OctoClient { return false } - const responseJson = (await response.json() || {}) as {token?: string} + const responseJson = (await this.getJson(response)) as {token?: string} this.token = responseJson.token if (responseJson.token !== '') { localStorage.setItem('sessionId', this.token || '') @@ -45,7 +54,7 @@ class OctoClient { return false } - async register(email: string, username: string, password: string, token?: string): Promise<200 | 401 | 500> { + async register(email: string, username: string, password: string, token?: string): Promise<{code: number, json: any}> { const path = '/api/v1/register' const body = JSON.stringify({email, username, password, token}) const response = await fetch(this.serverUrl + path, { @@ -53,10 +62,8 @@ class OctoClient { headers: this.headers(), body, }) - if (response.status === 200 || response.status === 401) { - return response.status - } - return 500 + const json = (await this.getJson(response)) + return {code: response.status, json} } private headers() { @@ -73,7 +80,7 @@ class OctoClient { if (response.status !== 200) { return undefined } - const user = (await response.json()) as IUser + const user = (await this.getJson(response)) as IUser return user } @@ -86,7 +93,7 @@ class OctoClient { if (response.status !== 200) { return [] } - const blocks = (await response.json() || []) as IMutableBlock[] + const blocks = (await this.getJson(response, [])) as IMutableBlock[] this.fixBlocks(blocks) return blocks } @@ -97,7 +104,7 @@ class OctoClient { if (response.status !== 200) { return [] } - const blocks = (await response.json() || []) as IMutableBlock[] + const blocks = (await this.getJson(response, [])) as IMutableBlock[] this.fixBlocks(blocks) return blocks } @@ -135,7 +142,7 @@ class OctoClient { if (response.status !== 200) { return [] } - const blocks = (await response.json() || []) as IMutableBlock[] + const blocks = (await this.getJson(response, [])) as IMutableBlock[] this.fixBlocks(blocks) return blocks } @@ -217,7 +224,7 @@ class OctoClient { Utils.log(`uploadFile response: ${text}`) const json = JSON.parse(text) - // const json = await response.json() + // const json = await this.getJson(response) return json.url } catch (e) { Utils.logError(`uploadFile json ERROR: ${e}`) @@ -237,7 +244,7 @@ class OctoClient { if (response.status !== 200) { return undefined } - const sharing = (await response.json()) as ISharing + const sharing = (await this.getJson(response)) as ISharing return sharing } @@ -267,7 +274,7 @@ class OctoClient { if (response.status !== 200) { return undefined } - const workspace = (await response.json()) as IWorkspace + const workspace = (await this.getJson(response)) as IWorkspace return workspace } diff --git a/webapp/src/pages/registerPage.tsx b/webapp/src/pages/registerPage.tsx index b51bf83a0..fe876ec99 100644 --- a/webapp/src/pages/registerPage.tsx +++ b/webapp/src/pages/registerPage.tsx @@ -32,16 +32,19 @@ class RegisterPage extends React.PureComponent { const queryString = new URLSearchParams(window.location.search) const signupToken = queryString.get('t') || '' - const registered = await client.register(this.state.email, this.state.username, this.state.password, signupToken) - if (registered === 200) { + const response = await client.register(this.state.email, this.state.username, this.state.password, signupToken) + if (response.code === 200) { const logged = await client.login(this.state.username, this.state.password) if (logged) { this.props.history.push('/') + + // HACKHACK: react-router-dom seems to require a refresh to navigate correctly + // this.setState({email: '', username: '', password: ''}) } - } else if (registered === 401) { + } else if (response.code === 401) { this.setState({errorMessage: 'Invalid registration link, please contact your administrator'}) } else { - this.setState({errorMessage: 'Server error'}) + this.setState({errorMessage: response.json.error}) } }