Guard for no person for user (#18812)

This commit is contained in:
Bram Kragten 2023-11-29 20:59:06 +01:00 committed by GitHub
parent bf24740c7b
commit b5b2392dd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 12 deletions

View File

@ -17,7 +17,7 @@ import {
submitLoginFlow, submitLoginFlow,
} from "../data/auth"; } from "../data/auth";
import { DataEntryFlowStep } from "../data/data_entry_flow"; import { DataEntryFlowStep } from "../data/data_entry_flow";
import { listPersons } from "../data/person"; import { BasePerson, listUserPersons } from "../data/person";
import "./ha-auth-textfield"; import "./ha-auth-textfield";
import type { HaAuthTextField } from "./ha-auth-textfield"; import type { HaAuthTextField } from "./ha-auth-textfield";
@ -43,7 +43,7 @@ export class HaLocalAuthFlow extends LitElement {
@state() private _submitting = false; @state() private _submitting = false;
@state() private _persons?: Promise<Record<string, string>>; @state() private _persons?: Record<string, BasePerson>;
@state() private _selectedUser?: string; @state() private _selectedUser?: string;
@ -65,7 +65,9 @@ export class HaLocalAuthFlow extends LitElement {
if (!this.authProvider?.users || !this._persons) { if (!this.authProvider?.users || !this._persons) {
return nothing; return nothing;
} }
const userIds = Object.keys(this.authProvider.users); const userIds = Object.keys(this.authProvider.users).filter(
(userId) => userId in this._persons!
);
return html` return html`
<style> <style>
.content { .content {
@ -198,9 +200,9 @@ export class HaLocalAuthFlow extends LitElement {
: this._selectedUser : this._selectedUser
? html`<div class="login-form"><div class="person"> ? html`<div class="login-form"><div class="person">
<ha-person-badge <ha-person-badge
.person=${this._persons![this._selectedUser]} .person=${this._persons[this._selectedUser]}
></ha-person-badge> ></ha-person-badge>
<p>${this._persons![this._selectedUser].name}</p> <p>${this._persons[this._selectedUser].name}</p>
</div> </div>
<form> <form>
<input <input
@ -273,6 +275,7 @@ export class HaLocalAuthFlow extends LitElement {
> >
${userIds.map((userId) => { ${userIds.map((userId) => {
const person = this._persons![userId]; const person = this._persons![userId];
return html`<div return html`<div
class="person" class="person"
.userId=${userId} .userId=${userId}
@ -316,7 +319,12 @@ export class HaLocalAuthFlow extends LitElement {
} }
private async _load() { private async _load() {
this._persons = await (await listPersons()).json(); try {
this._persons = await listUserPersons();
} catch {
this._persons = {};
this._error = "Failed to fetch persons";
}
} }
private _restart() { private _restart() {

View File

@ -2,12 +2,12 @@ import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import { classMap } from "lit/directives/class-map"; import { classMap } from "lit/directives/class-map";
import { styleMap } from "lit/directives/style-map"; import { styleMap } from "lit/directives/style-map";
import { Person } from "../../data/person"; import { BasePerson } from "../../data/person";
import { computeUserInitials } from "../../data/user"; import { computeUserInitials } from "../../data/user";
@customElement("ha-person-badge") @customElement("ha-person-badge")
class PersonBadge extends LitElement { class PersonBadge extends LitElement {
@property({ attribute: false }) public person?: Person; @property({ attribute: false }) public person?: BasePerson;
protected render() { protected render() {
if (!this.person) { if (!this.person) {

View File

@ -1,11 +1,14 @@
import { HomeAssistant } from "../types"; import { HomeAssistant } from "../types";
export interface Person { export interface BasePerson {
id: string;
name: string; name: string;
picture?: string;
}
export interface Person extends BasePerson {
id: string;
user_id?: string; user_id?: string;
device_trackers?: string[]; device_trackers?: string[];
picture?: string;
} }
export interface PersonMutableParams { export interface PersonMutableParams {
@ -21,9 +24,14 @@ export const fetchPersons = (hass: HomeAssistant) =>
config: Person[]; config: Person[];
}>({ type: "person/list" }); }>({ type: "person/list" });
export const listPersons = () => export const listUserPersons = (): Promise<Record<string, BasePerson>> =>
fetch("/api/person/list", { fetch("/api/person/list", {
credentials: "same-origin", credentials: "same-origin",
}).then((resp) => {
if (resp.ok) {
return resp.json();
}
throw new Error(resp.statusText);
}); });
export const createPerson = ( export const createPerson = (