From 6125329cae0312be2c6f00f42d48e54b99414b3c Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 6 Sep 2018 14:39:59 -0400 Subject: [PATCH] show authorize form when auth error --- embark-ui/src/actions/index.js | 2 +- embark-ui/src/containers/AppContainer.js | 27 +++++++++++++++++++++++- embark-ui/src/index.js | 2 +- embark-ui/src/services/api.js | 10 +++++++-- lib/modules/authenticator/index.js | 2 +- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/embark-ui/src/actions/index.js b/embark-ui/src/actions/index.js index 9a18f1ab..983f6547 100644 --- a/embark-ui/src/actions/index.js +++ b/embark-ui/src/actions/index.js @@ -15,7 +15,7 @@ function action(type, payload = {}) { export const AUTHENTICATE = createRequestTypes('AUTHENTICATE'); export const authenticate = { - request: (token) => action(AUTHENTICATE[REQUEST], {token}), + request: (token, callback) => action(AUTHENTICATE[REQUEST], {token, callback}), success: () => action(AUTHENTICATE[SUCCESS]), failure: (error) => action(AUTHENTICATE[FAILURE], {error}) }; diff --git a/embark-ui/src/containers/AppContainer.js b/embark-ui/src/containers/AppContainer.js index 78ec7759..b7b354ae 100644 --- a/embark-ui/src/containers/AppContainer.js +++ b/embark-ui/src/containers/AppContainer.js @@ -2,6 +2,7 @@ import PropTypes from "prop-types"; import {connect} from 'react-redux'; import React, {Component} from 'react'; import {withRouter} from "react-router-dom"; +import {Alert, Page, Form, Button} from "tabler-react"; import routes from '../routes'; import queryString from 'query-string'; @@ -16,6 +17,12 @@ import { } from '../actions'; class AppContainer extends Component { + constructor (props) { + super(props); + this.state = { + authenticateError: null + }; + } componentDidMount() { let token; if (this.props.location.search) { @@ -24,7 +31,12 @@ class AppContainer extends Component { } else { token = cacheGet('token'); } - this.props.authenticate(token); + this.props.authenticate(token, (err) => { + if (err) { + return this.setState({authenticateError: err}); + } + this.setState({authenticateError: null}); + }); this.props.initBlockHeader(); this.props.fetchProcesses(); this.props.fetchVersions(); @@ -32,6 +44,19 @@ class AppContainer extends Component { } render() { + if (this.state.authenticateError) { + return + + {this.state.authenticateError} + +
+ + + +
; + } return ({routes}); } } diff --git a/embark-ui/src/index.js b/embark-ui/src/index.js index f956ca2b..7f69eded 100644 --- a/embark-ui/src/index.js +++ b/embark-ui/src/index.js @@ -19,7 +19,7 @@ ReactDOM.render( - + , diff --git a/embark-ui/src/services/api.js b/embark-ui/src/services/api.js index d6ac5ba4..3717006a 100644 --- a/embark-ui/src/services/api.js +++ b/embark-ui/src/services/api.js @@ -11,12 +11,18 @@ function get(path, params, endpoint) { } function post(path, params) { + const callback = params.callback || function(){}; + delete params.callback; return axios.post(constants.httpEndpoint + path, params) .then((response) => { - return {response, error: null}; + const data = (response.data && response.data.error) ? {error: response.data.error} : {response, error: null}; + callback(data.error, data.response); + return data; }) .catch((error) => { - return {response: null, error: error.message || 'Something bad happened'}; + const data = {response: null, error: error.message || 'Something bad happened'}; + callback(data.error, data.response); + return data; }); } diff --git a/lib/modules/authenticator/index.js b/lib/modules/authenticator/index.js index 0ae53e70..b68d2491 100644 --- a/lib/modules/authenticator/index.js +++ b/lib/modules/authenticator/index.js @@ -23,7 +23,7 @@ class Authenticator { this.logger.warn(__('Someone tried and failed to authenticate to the backend')); this.logger.warn(__('- User-Agent: %s', req.headers['user-agent'])); this.logger.warn(__('- Referer: %s', req.headers.referer)); - return res.status(403).send({error: __('Wrong authentication token')}); + return res.send({error: __('Wrong authentication token. Get your token from the Embark console by typing `token`')}); } res.send(); }