From ac20cf292af0764a1bb6b0bbfcc19f930fe12ed9 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 8 Aug 2023 14:43:55 +0200 Subject: [PATCH] Auth: Make it clearer where you are logging in to (#17459) --- src/auth/ha-authorize.ts | 112 ++++++++++++++++++++++++++++----------- src/translations/en.json | 4 +- 2 files changed, 84 insertions(+), 32 deletions(-) diff --git a/src/auth/ha-authorize.ts b/src/auth/ha-authorize.ts index 91fed8ef07..19e760f9d1 100644 --- a/src/auth/ha-authorize.ts +++ b/src/auth/ha-authorize.ts @@ -1,8 +1,16 @@ -import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit"; +import { + css, + CSSResultGroup, + html, + LitElement, + nothing, + PropertyValues, +} from "lit"; import { customElement, property, state } from "lit/decorators"; import punycode from "punycode"; import { applyThemesOnElement } from "../common/dom/apply_themes_on_element"; import { extractSearchParamsObject } from "../common/url/search-params"; +import "../components/ha-alert"; import { AuthProvider, AuthUrlSearchParams, @@ -14,6 +22,11 @@ import "./ha-auth-flow"; import("./ha-pick-auth-provider"); +const appNames = { + "https://home-assistant.io/iOS": "iOS", + "https://home-assistant.io/android": "Android", +}; + @customElement("ha-authorize") export class HaAuthorize extends litLocalizeLiteMixin(LitElement) { @property() public clientId?: string; @@ -26,6 +39,10 @@ export class HaAuthorize extends litLocalizeLiteMixin(LitElement) { @state() private _authProviders?: AuthProvider[]; + @state() private _ownInstance = false; + + @state() private _error?: string; + constructor() { super(); this.translationFragment = "page-authorize"; @@ -42,39 +59,47 @@ export class HaAuthorize extends litLocalizeLiteMixin(LitElement) { } protected render() { + if (this._error) { + return html`${this._error} ${this.redirectUri}`; + } + if (!this._authProviders) { return html`

${this.localize("ui.panel.page-authorize.initializing")}

`; } - // We don't have a good approach yet to map text markup in localization. - // So we sanitize the translation with innerText and then inject - // the name with a bold tag. - const loggingInWith = document.createElement("div"); - loggingInWith.innerText = this.localize( - "ui.panel.page-authorize.logging_in_with", - "authProviderName", - "NAME" - ); - loggingInWith.innerHTML = loggingInWith.innerHTML.replace( - "**NAME**", - `${this._authProvider!.name}` - ); - const inactiveProviders = this._authProviders.filter( (prv) => prv !== this._authProvider ); + const app = this.clientId && this.clientId in appNames; + return html` -

- ${this.localize( - "ui.panel.page-authorize.authorizing_client", - "clientId", - this.clientId ? punycode.toASCII(this.clientId) : this.clientId - )} -

- ${loggingInWith} + ${!this._ownInstance + ? html` + ${app + ? this.localize("ui.panel.page-authorize.authorizing_app", { + app: appNames[this.clientId!], + }) + : this.localize("ui.panel.page-authorize.authorizing_client", { + clientId: html`${this.clientId + ? punycode.toASCII(this.clientId) + : this.clientId}`, + })} + ` + : html`

${this.localize("ui.panel.page-authorize.authorizing")}

`} + ${inactiveProviders.length > 0 + ? html`

+ ${this.localize("ui.panel.page-authorize.logging_in_with", { + authProviderName: html`${this._authProvider!.name}`, + })} +

` + : nothing}