2023-11-27 09:32:17 -05:00
|
|
|
import { Accounts } from "./accounts.js";
|
|
|
|
|
2023-11-23 11:26:09 -05:00
|
|
|
class Client
|
|
|
|
{
|
|
|
|
constructor(url)
|
|
|
|
{
|
|
|
|
this._url = url;
|
2023-11-27 09:32:17 -05:00
|
|
|
this.accounts = new Accounts(this);
|
2023-11-29 10:05:49 -05:00
|
|
|
this._logged = undefined;
|
2023-11-23 11:26:09 -05:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:05:49 -05:00
|
|
|
async isAuthentificate()
|
2023-11-23 11:26:09 -05:00
|
|
|
{
|
2023-11-29 10:05:49 -05:00
|
|
|
if (this._logged == undefined)
|
|
|
|
this.logged = await this._test_logged();
|
|
|
|
return this.logged;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _get(uri)
|
|
|
|
{
|
|
|
|
let response = await fetch(this._url + uri, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
return response;
|
2023-11-23 11:26:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async _post(uri, data)
|
|
|
|
{
|
|
|
|
let response = await fetch(this._url + uri, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2023-11-29 10:05:49 -05:00
|
|
|
},
|
2023-11-23 11:26:09 -05:00
|
|
|
body: JSON.stringify(data),
|
|
|
|
});
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
async login(username, password)
|
|
|
|
{
|
2023-11-27 09:32:17 -05:00
|
|
|
let response = await this._post("/api/accounts/login", {username: username, password: password})
|
2023-11-29 10:05:49 -05:00
|
|
|
let data = await response.json();
|
|
|
|
if (data == "user connected")
|
|
|
|
{
|
|
|
|
this.logged = true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async logout()
|
|
|
|
{
|
|
|
|
await this._get("/api/accounts/logout");
|
|
|
|
this.logged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _test_logged()
|
|
|
|
{
|
|
|
|
let response = await this._get("/api/accounts/logged");
|
|
|
|
let data = await response.json();
|
|
|
|
return data === "True";
|
2023-11-23 11:26:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 04:53:34 -05:00
|
|
|
export {Client}
|