import { html } from "lit"; import { customElement } from "lit/decorators.js"; import { tempTokenLogin } from "./Auth"; import { BaseModal } from "./components/BaseModal"; import "./components/Difficulties"; import "./components/PatternButton"; import { modalHeader } from "./components/ui/ModalHeader"; import { translateText } from "./Utils"; @customElement("token-login") export class TokenLoginModal extends BaseModal { private isAttemptingLogin = false; private retryInterval: NodeJS.Timeout | undefined = undefined; private token: string | null = null; private email: string | null = null; private attemptCount = 0; constructor() { super(); } render() { const title = translateText("token_login_modal.title"); const content = html`
${modalHeader({ title, onBack: () => this.close(), ariaLabel: translateText("common.back"), })}
${this.email ? this.loginSuccess(this.email) : this.loggingIn()}
`; if (this.inline) { return content; } return html` ${content} `; } private loggingIn() { const loggingText = translateText("token_login_modal.logging_in"); return html`

${loggingText}

`; } private loginSuccess(email: string) { const successText = translateText("token_login_modal.success", { email }); return html`

${successText}

`; } public open(): void { if (!this.token) { return; } super.open(); clearInterval(this.retryInterval); this.retryInterval = setInterval(() => this.tryLogin(), 3000); } public openWithToken(token: string): void { this.token = token; this.email = null; this.attemptCount = 0; this.isAttemptingLogin = false; this.open(); } public close() { this.token = null; clearInterval(this.retryInterval); this.attemptCount = 0; super.close(); this.isAttemptingLogin = false; } private async tryLogin() { if (this.isAttemptingLogin) { return; } if (this.attemptCount > 3) { this.close(); alert("Login failed. Please try again later."); return; } this.attemptCount++; this.isAttemptingLogin = true; if (this.token === null) { this.close(); return; } try { this.email = await tempTokenLogin(this.token); if (!this.email) { return; } clearInterval(this.retryInterval); setTimeout(() => { this.close(); window.location.reload(); }, 1000); this.requestUpdate(); } catch (e) { console.error(e); } finally { this.isAttemptingLogin = false; } } }