From 6d18a434c47d6dd3b64e59bb1c0ee75fc86d3831 Mon Sep 17 00:00:00 2001 From: Timmo Date: Sat, 30 Mar 2019 13:53:56 +0000 Subject: [PATCH] :hammer: Fix HA auth check at login --- src/App.test.js | 1 - src/Components/Main.js | 1 - src/Components/Root.js | 21 +++++++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/App.test.js b/src/App.test.js index 2dca3d352..296f0f194 100644 --- a/src/App.test.js +++ b/src/App.test.js @@ -11,7 +11,6 @@ it('renders without crashing', () => { it('logs in without crashing', () => { localStorage.setItem('username', process.env.REACT_APP_OVERRIDE_USERNAME); localStorage.setItem('password', process.env.REACT_APP_OVERRIDE_PASSWORD); - localStorage.setItem('api_url', process.env.REACT_APP_OVERRIDE_API_URL); localStorage.setItem('hass_url', process.env.REACT_APP_OVERRIDE_HASS_URL); const div = document.createElement('div'); ReactDOM.render(, div); diff --git a/src/Components/Main.js b/src/Components/Main.js index e0fa6b7b0..796fb7d47 100644 --- a/src/Components/Main.js +++ b/src/Components/Main.js @@ -49,7 +49,6 @@ class Main extends React.PureComponent { }; componentDidMount = () => { - this.props.setTheme(); this.onMouseMoveHandler(); }; diff --git a/src/Components/Root.js b/src/Components/Root.js index 91795ca1a..ceda035a6 100644 --- a/src/Components/Root.js +++ b/src/Components/Root.js @@ -20,7 +20,6 @@ import { CircularProgress, Typography } from '@material-ui/core'; import Login from './Login'; import Main from './Main'; import defaultConfig from './EditConfig/defaultConfig.json'; -// import cleanupObject from './Common/cleanupObject'; const styles = theme => ({ root: { @@ -92,7 +91,10 @@ class Root extends React.PureComponent { setTimeout(() => this.setState({ loginAttempted: true }), 500); }; - logout = () => + logout = () => { + localStorage.removeItem('username'); + localStorage.removeItem('password'); + localStorage.removeItem('hass_tokens'); app.logout().then(() => this.setState({ loggedIn: false, @@ -100,6 +102,7 @@ class Root extends React.PureComponent { config: undefined }) ); + }; login = (data = undefined) => { process.env.NODE_ENV === 'development' && console.log('login:', data); @@ -175,6 +178,9 @@ class Root extends React.PureComponent { if (config.theme && config.theme.custom) config.theme.custom.map(theme => this.props.addTheme(theme)); }); + + this.setTheme(); + configService.on('updated', () => this.getConfig()); configService.on('patched', () => this.getConfig()); }; @@ -182,10 +188,9 @@ class Root extends React.PureComponent { loggedIn = () => { if (this.state.hass_url) { if (this.loadTokens()) this.connectToHASS(); - else if (localStorage.getItem('should_auth')) { - if (!localStorage.getItem('auth_triggered')) this.connectToHASS(); - else this.askAuth(); - } + else if (this.props.location.search.includes('auth_callback=1')) + this.connectToHASS(); + else this.askAuth(); } else this.setState({ entities: [], @@ -204,6 +209,8 @@ class Root extends React.PureComponent { try { hassTokens = JSON.parse(localStorage.getItem('hass_tokens')); } catch (err) {} // eslint-disable-line + process.env.NODE_ENV === 'development' && + console.log('loadTokens:', hassTokens); return hassTokens; }; @@ -252,6 +259,7 @@ class Root extends React.PureComponent { }; connectToHASS = () => { + process.env.NODE_ENV === 'development' && console.log('connectToHASS'); (async () => { localStorage.setItem('auth_triggered', true); connection = this.authProm().then(this.connProm); @@ -349,6 +357,7 @@ class Root extends React.PureComponent { this.setState({ entities: Object.entries(entities) }); setTheme = (themeId = undefined) => { + process.env.NODE_ENV === 'development' && console.log('setTheme:', themeId); const { config } = this.state; const lightThemeName = config.theme.auto && config.theme.auto.light_theme