import Conquer from '@burguillosinfo/conquer' import ConquerInterfaceManager from '@burguillosinfo/conquer/interface-manager' import LoginUI from '@burguillosinfo/conquer/interface/login' export type ConquerLoginEventCallback = () => void export default class Login { private conquerLogin: HTMLDivElement private conquerInterfaceManager: ConquerInterfaceManager private cachedIsLoggedIn: boolean | null = null constructor(conquerInterfaceManager: ConquerInterfaceManager) { this.conquerInterfaceManager = conquerInterfaceManager } public async start(): Promise { this.loopCheckLogin() } public async onRegisterRequest(loginUI: LoginUI, username: string, password: string, repeatPassword: string): Promise { const urlUser = new URL('/conquer/user', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port) let responseJson let status try { const response = await fetch(urlUser, { method: 'PUT', body: JSON.stringify({ username: username, password: password, repeat_password: repeatPassword }) }) responseJson = await response.json() status = response.status } catch(e) { console.error(e) loginUI.addNewRegisterError('El servidor ha enviado datos inesperados.') return } if (status !== 200) { loginUI.addNewRegisterError(responseJson.error) return } loginUI.addNewLoginSuccessText(`Usuario registrado ${username}.`) loginUI.goToLogin() } private async loopCheckLogin(): Promise { window.setInterval(() => { this.isLogged().then((isLogged) => { if (isLogged) { if (this.cachedIsLoggedIn !== true) { this.cachedIsLoggedIn = true; this.onLoginSuccess(); } return; } if (this.cachedIsLoggedIn !== false) { this.cachedIsLoggedIn = false; this.onLogout() } }) }, 5000) } private async onLogout(): Promise { const interfaceManager = this.conquerInterfaceManager const loginUI = new LoginUI(this) for (const callback of this.callbacks.logout) { callback(); } loginUI.on('close', () => { interfaceManager.remove(loginUI); }) interfaceManager.push(loginUI) } private callbacks: Record> = {} public async on(name: string, callback: ConquerLoginEventCallback) { if (this.callbacks[name] === undefined) { this.callbacks[name] = [] } this.callbacks[name].push(callback) } private async onLoginSuccess(): Promise { this.cachedIsLoggedIn = true for (const callback of this.callbacks.login) { callback() } } public async onLoginRequested(loginUI: LoginUI, username: string, password: string): Promise { const urlUser = new URL('/conquer/user/login', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port) let responseJson let status try { const response = await fetch(urlUser, { method: 'POST', body: JSON.stringify({ username: username, password: password, }) }) responseJson = await response.json() status = response.status } catch(e) { console.error(e) loginUI.addNewLoginError('El servidor ha enviado datos inesperados.') return } if (status !== 200) { loginUI.addNewLoginError(responseJson.error) return } loginUI.unsetLoginAndRegisterErrors() const isLogged = await this.isLogged() if (isLogged) { this.onLoginSuccess() } } public async isLogged(): Promise { const urlUser = new URL('/conquer/user', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port) let status try { const response = await fetch(urlUser) status = response.status } catch { return false } return status === 200 } }