Skip to content

Commit

Permalink
🔨 Fix HA auth check at login
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 committed Mar 30, 2019
1 parent 10b8938 commit 6d18a43
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<App />, div);
Expand Down
1 change: 0 additions & 1 deletion src/Components/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class Main extends React.PureComponent {
};

componentDidMount = () => {
this.props.setTheme();
this.onMouseMoveHandler();
};

Expand Down
21 changes: 15 additions & 6 deletions src/Components/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -92,14 +91,18 @@ 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,
loginError: undefined,
config: undefined
})
);
};

login = (data = undefined) => {
process.env.NODE_ENV === 'development' && console.log('login:', data);
Expand Down Expand Up @@ -175,17 +178,19 @@ 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());
};

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: [],
Expand All @@ -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;
};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6d18a43

Please sign in to comment.