ft_transcendence/frontend/static/js/api/account.js

72 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-12-16 12:00:38 -05:00
import { Client } from "./client.js";
2023-11-30 19:29:56 -05:00
class Account
{
2023-12-16 12:00:38 -05:00
/**
* @param {Client} client
*/
2023-11-30 19:29:56 -05:00
constructor (client)
{
2023-12-16 12:00:38 -05:00
/**
* @type {Client} client
*/
2023-11-30 19:29:56 -05:00
this.client = client;
}
async create(username, password)
{
let response = await this.client._post("/api/accounts/register", {username: username, password: password});
let response_data = await response.json()
if (response_data == "user created")
{
this._logged = true;
return null;
}
return response_data
}
async delete(password)
{
let response = await this.client._delete("/api/accounts/delete", {password: password});
let response_data = await response.json();
2023-12-04 06:32:15 -05:00
if (JSON.stringify(response_data) == JSON.stringify({'detail': 'Authentication credentials were not provided.'}))
{
2023-12-20 13:10:26 -05:00
this.client._update_logged(false);
2023-12-04 06:32:15 -05:00
return null;
}
if (response_data == "user deleted")
2023-11-30 19:29:56 -05:00
this.client._logged = false;
return response_data;
}
async get()
{
let response = await this.client._get("/api/accounts/edit");
let response_data = await response.json();
if (JSON.stringify(response_data) == JSON.stringify({'detail': 'Authentication credentials were not provided.'}))
{
2023-12-04 06:32:15 -05:00
this.client._logged = false;
2023-11-30 19:29:56 -05:00
return null;
}
return response_data;
}
async update(data, password)
{
2023-12-04 06:32:15 -05:00
data.current_password = password;
2023-11-30 19:29:56 -05:00
let response = await this.client._patch_json("/api/accounts/edit", data);
let response_data = await response.json();
if (JSON.stringify(response_data) == JSON.stringify({'detail': 'Authentication credentials were not provided.'}))
{
2023-12-16 12:00:38 -05:00
this.client._;
2023-11-30 19:29:56 -05:00
return null;
}
return response_data;
}
}
export { Account }