browsertrix/frontend/src/utils/auth.ts
sua yoo 5531c3c33c
hotfix: fix auth persist
Switch lifecycle event to update
2021-12-05 16:52:58 -08:00

36 lines
833 B
TypeScript

import { DASHBOARD_ROUTE } from "../routes";
import LiteElement from "../utils/LiteElement";
import type { AuthState } from "../utils/AuthService";
import type { CurrentUser } from "../types/user";
/**
* Block rendering and dispatch event if user is not logged in
*
* Usage example:
* ```ts
* @needLogin
* MyComponent extends LiteElement {}
* ```
*/
export function needLogin<T extends { new (...args: any[]): LiteElement }>(
constructor: T
) {
return class extends constructor {
authState?: AuthState;
static get properties() {
return {
authState: { type: Object },
};
}
update(changedProperties: Map<string, any>) {
if (this.authState) {
super.update(changedProperties);
} else {
this.dispatchEvent(new CustomEvent("need-login"));
}
}
};
}