diff --git a/src/entrypoints/core.js b/src/entrypoints/core.js
index adb0113d8d..08cf473952 100644
--- a/src/entrypoints/core.js
+++ b/src/entrypoints/core.js
@@ -4,6 +4,7 @@ import {
subscribeConfig,
subscribeEntities,
subscribeServices,
+ ERR_INVALID_AUTH,
} from 'home-assistant-js-websocket';
import { loadTokens, saveTokens } from '../common/auth/token_storage.js';
@@ -12,27 +13,44 @@ import { subscribeThemes } from '../data/ws-themes.js';
import { subscribeUser } from '../data/ws-user.js';
const hassUrl = `${location.protocol}//${location.host}`;
+const isExternal = location.search.includes('external_auth=1');
-if (location.search.includes('external_auth=1')) {
- window.hassAuth = import('../common/auth/external_auth.js')
- .then(mod => new mod.default(hassUrl));
-} else {
- window.hassAuth = getAuth({
+const authProm = isExternal ?
+ () => import('../common/auth/external_auth.js')
+ .then(mod => new mod.default(hassUrl)) :
+ () => getAuth({
hassUrl,
saveTokens,
loadTokens: () => Promise.resolve(loadTokens()),
});
-}
-window.hassConnection = window.hassAuth.then((auth) => {
- if (location.search.includes('auth_callback=1')) {
- history.replaceState(null, null, location.pathname);
+const connProm = async (auth) => {
+ try {
+ const conn = await createConnection({ auth });
+
+ // Clear url if we have been able to establish a connection
+ if (location.search.includes('auth_callback=1')) {
+ history.replaceState(null, null, location.pathname);
+ }
+
+ return { auth, conn };
+ } catch (err) {
+ if (err !== ERR_INVALID_AUTH) {
+ throw err;
+ }
+ // We can get invalid auth if auth tokens were stored that are no longer valid
+ // Clear stored tokens.
+ if (!isExternal) saveTokens(null);
+ auth = await authProm();
+ const conn = await createConnection({ auth });
+ return { auth, conn };
}
- return createConnection({ auth });
-});
+};
+
+window.hassConnection = authProm().then(connProm);
// Start fetching some of the data that we will need.
-window.hassConnection.then((conn) => {
+window.hassConnection.then(({ conn }) => {
const noop = () => {};
subscribeEntities(conn, noop);
subscribeConfig(conn, noop);
diff --git a/src/layouts/app/connection-mixin.js b/src/layouts/app/connection-mixin.js
index 78cebb5eb8..6cd8663b27 100644
--- a/src/layouts/app/connection-mixin.js
+++ b/src/layouts/app/connection-mixin.js
@@ -25,7 +25,16 @@ export default superClass =>
}
async _handleConnProm() {
- const [auth, conn] = await Promise.all([window.hassAuth, window.hassConnection]);
+ let auth;
+ let conn;
+ try {
+ const result = await window.hassConnection;
+ auth = result.auth;
+ conn = result.conn;
+ } catch (err) {
+ this._error = true;
+ return;
+ }
this.hass = Object.assign({
auth,
diff --git a/src/layouts/app/home-assistant.js b/src/layouts/app/home-assistant.js
index 7bba5ad5b7..a52489fc1c 100644
--- a/src/layouts/app/home-assistant.js
+++ b/src/layouts/app/home-assistant.js
@@ -51,7 +51,7 @@ class HomeAssistant extends ext(PolymerElement, [
-