Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support external step for login flow #23204

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/auth/ha-auth-external.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { CSSResultGroup, TemplateResult } from "lit";
import { css, html } from "lit";
import { customElement, property } from "lit/decorators";
import type { DataEntryFlowStepExternal } from "../data/data_entry_flow";
import { fireEvent } from "../common/dom/fire_event";
import type { LocalizeFunc } from "../common/translations/localize";
import { HaForm } from "../components/ha-form/ha-form";

@customElement("ha-auth-external")
export class HaAuthExternal extends HaForm {
@property({ attribute: false }) public localize!: LocalizeFunc;

@property({ attribute: false }) public stepTitle?: string;

@property({ attribute: false }) public step!: DataEntryFlowStepExternal;

protected render(): TemplateResult {
return html`
<h2>${this.stepTitle}</h2>
<div class="content">
${this.localize("ui.panel.page-authorize.external.description")}
<div class="open-button">
<a href=${this.step.url} target="_blank" rel="opener">
<mwc-button raised>
${this.localize("ui.panel.page-authorize.external.open_site")}
</mwc-button>
</a>
</div>
</div>
`;
}

protected firstUpdated(changedProps) {
super.firstUpdated(changedProps);
window.open(this.step.url);
window.addEventListener("message", async (message: MessageEvent) => {
if (message.data.type === "externalCallback") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do a check for origin and source of the message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sounds good. I have this working for the window.open (following what the previous PR did). How do you track the source of the button that opens the window above?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think there is a way with just an anchor link. You could make it a click event and open the window programmatically with window.open. Hoping that will not cause issues with popup blockers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK -- I changed this to use a button to run the same code thats run when first loading. Perhaps that is reasonable...

fireEvent(this, "step-finished");
}
});
}

static get styles(): CSSResultGroup {
return [
css`
.open-button {
text-align: center;
padding: 24px 0;
}
.open-button a {
text-decoration: none;
}
`,
];
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-auth-external": HaAuthExternal;
}
interface HASSDomEvents {
"step-finished": undefined;
}
}
44 changes: 31 additions & 13 deletions src/auth/ha-auth-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
DataEntryFlowStepForm,
} from "../data/data_entry_flow";
import "./ha-auth-form";
import "./ha-auth-external";

type State = "loading" | "error" | "step";

Expand Down Expand Up @@ -171,18 +172,7 @@ export class HaAuthFlow extends LitElement {
}

return html`
${this._renderStep(this.step)}
<div class="action">
<mwc-button
raised
@click=${this._handleSubmit}
.disabled=${this._submitting}
>
${this.step.type === "form"
? this.localize("ui.panel.page-authorize.form.next")
: this.localize("ui.panel.page-authorize.form.start_over")}
</mwc-button>
</div>
${this._renderStep(this.step)} ${this._renderAction(this.step)}
`;
case "error":
return html`
Expand Down Expand Up @@ -266,11 +256,35 @@ export class HaAuthFlow extends LitElement {
`
: ""}
`;
case "external":
return html`<ha-auth-external
.localize=${this.localize}
.step=${this.step!}
.title=${this.authProvider!.name}
@step-finished=${this._externalStepFinished}
></ha-auth-external>`;
default:
return nothing;
}
}

private _renderAction(step: DataEntryFlowStep) {
if (step.type === "external") {
return nothing;
}
return html` <div class="action">
bramkragten marked this conversation as resolved.
Show resolved Hide resolved
<mwc-button
raised
@click=${this._handleSubmit}
.disabled=${this._submitting}
>
${step.type === "form"
? this.localize("ui.panel.page-authorize.form.next")
: this.localize("ui.panel.page-authorize.form.start_over")}
</mwc-button>
</div>`;
}

private _storeTokenChanged(e: CustomEvent<HTMLInputElement>) {
this._storeToken = (e.currentTarget as HTMLInputElement).checked;
}
Expand Down Expand Up @@ -364,7 +378,7 @@ export class HaAuthFlow extends LitElement {
if (this.step == null) {
return;
}
if (this.step.type !== "form") {
if (this.step.type !== "form" && this.step.type !== "external") {
this._providerChanged(this.authProvider);
return;
}
Expand Down Expand Up @@ -403,6 +417,10 @@ export class HaAuthFlow extends LitElement {
this._submitting = false;
}
}

private _externalStepFinished(ev: CustomEvent) {
this._handleSubmit(ev);
allenporter marked this conversation as resolved.
Show resolved Hide resolved
}
}

declare global {
Expand Down
4 changes: 4 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7144,6 +7144,10 @@
}
}
}
},
"external": {
"description": "This step requires you to visit an external website to be completed.",
"open_site": "Open website"
}
},
"page-demo": {
Expand Down
Loading