"use strict"; function byId(id, nullOk=false) { const el = document.getElementById(id); if (!el && !nullOk) { console.error(`No element #${id}`); } return el; } class MattermostApi { constructor(endpoint) { this.endpoint = endpoint; } async validateToken(token) { const response = await ajax.getJson(`${this.endpoint}/users/me`, { headers: { "Token": `Authorization: Bearer ${token}` } }); return response.ok; } logIn(username, password) { return ajax.postJson(`${this.endpoint}/users/login`, { "login_id": username, "password": password }) .then(response => { let token = response.getHeader("Token"); window.localStorage.setItem("token", token); return response; }) .then(response => { document.body.innerHTML = ""; const pre = document.createElement("pre"); pre.innerText = JSON.stringify(response.json, null, 2); document.body.appendChild(pre); return response; }) .catch(error => { console.error(error); document.body.innerText = `An error occurred: ${error}`; }); } } function logIn() { let endpoint = byId("server").value; if (!endpoint.endsWith("/")) endpoint += "/"; endpoint += "api/v4"; let api = new MattermostApi(endpoint); api.logIn(byId("username").value, byId("password").value); }