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 = true 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 && this.cachedIsLoggedIn) { this.onLogout() this.cachedIsLoggedIn = false } }) }, 5000) } private async onLogout(): Promise { const interfaceManager = this.conquerInterfaceManager const loginUI = new LoginUI(this) 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(loginUI: LoginUI): Promise { this.cachedIsLoggedIn = true this.conquerInterfaceManager.remove(loginUI) 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(loginUI) } } 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 } }