2021-04-14 10:30:52 +02:00
|
|
|
import Cookies from "js-cookie";
|
|
|
|
|
|
|
|
class APIError extends Error {
|
|
|
|
constructor(message: string, public readonly errors: unknown) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Model {
|
|
|
|
static async fetch(
|
|
|
|
method: "get" | "post",
|
|
|
|
endpoint: string,
|
|
|
|
item_callback: (item: any) => any
|
|
|
|
) {
|
|
|
|
const init = {
|
|
|
|
headers: new Headers(),
|
|
|
|
method: method,
|
|
|
|
//body: JSON.stringify({})
|
|
|
|
};
|
|
|
|
const csrfToken = Cookies.get("csrftoken");
|
|
|
|
if (csrfToken != undefined) {
|
|
|
|
init.headers.set("X-CSRFToken", csrfToken);
|
|
|
|
}
|
|
|
|
init.headers.set("Accept", "application/json");
|
|
|
|
init.headers.set("Content-Type", "application/json");
|
|
|
|
const response = await fetch(`/api/${endpoint}`, init);
|
|
|
|
const dataOrErrors: Array<any> = await response.json();
|
|
|
|
if (response.status === 200) {
|
|
|
|
return dataOrErrors.map(item_callback);
|
|
|
|
} else {
|
|
|
|
throw new APIError(response.statusText, dataOrErrors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 09:31:03 +02:00
|
|
|
static async create(endpoint: string, data: any) {
|
2021-04-14 10:30:52 +02:00
|
|
|
const init = {
|
|
|
|
headers: new Headers(),
|
|
|
|
method: "post",
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
};
|
|
|
|
const csrfToken = Cookies.get("csrftoken");
|
|
|
|
if (csrfToken != undefined) {
|
|
|
|
init.headers.set("X-CSRFToken", csrfToken);
|
|
|
|
}
|
|
|
|
init.headers.set("Accept", "application/json");
|
|
|
|
init.headers.set("Content-Type", "application/json");
|
|
|
|
const response = await fetch(`/api/${endpoint}/`, init);
|
2021-04-15 10:05:54 +02:00
|
|
|
const dataOrErrors = await response.json();
|
|
|
|
if (response.status === 200) {
|
|
|
|
return dataOrErrors;
|
|
|
|
} else {
|
2021-04-14 10:30:52 +02:00
|
|
|
throw new APIError(response.statusText, dataOrErrors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UserData {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
2021-04-15 11:36:21 +02:00
|
|
|
username: string | undefined;
|
|
|
|
confidant_email: string | undefined;
|
2021-04-14 10:30:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class User extends Model implements UserData {
|
2021-04-15 10:05:54 +02:00
|
|
|
public isAuthenticated = false;
|
|
|
|
private token = "";
|
|
|
|
|
2021-04-15 11:36:21 +02:00
|
|
|
constructor(
|
|
|
|
public email: string,
|
|
|
|
public password: string,
|
|
|
|
public username: string | undefined,
|
|
|
|
public confidant_email: string | undefined
|
|
|
|
) {
|
2021-04-14 10:30:52 +02:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2021-04-15 09:31:03 +02:00
|
|
|
static async confirm(uid: string, token: string): Promise<void> {
|
|
|
|
await super.create("users/activation", { uid, token });
|
|
|
|
}
|
|
|
|
|
2021-04-14 11:21:39 +02:00
|
|
|
async login(): Promise<void> {
|
2021-04-15 10:05:54 +02:00
|
|
|
const response = await Model.create("token/login", {
|
2021-04-14 11:21:39 +02:00
|
|
|
email: this.email,
|
|
|
|
password: this.password,
|
|
|
|
});
|
2021-04-15 10:05:54 +02:00
|
|
|
this.token = response.auth_token;
|
|
|
|
this.isAuthenticated = true;
|
2021-04-14 11:21:39 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 10:30:52 +02:00
|
|
|
async signup(): Promise<void> {
|
2021-04-15 09:31:03 +02:00
|
|
|
await Model.create("users", { email: this.email, password: this.password });
|
2021-04-14 10:30:52 +02:00
|
|
|
}
|
|
|
|
}
|