2021-04-14 10:30:52 +02:00
|
|
|
import Cookies from "js-cookie";
|
|
|
|
|
2021-04-15 16:04:22 +02:00
|
|
|
type HTTPMethod = "GET" | "POST" | "PUT" | "PATCH";
|
|
|
|
|
2021-05-18 11:06:02 +02:00
|
|
|
export class APIError extends Error {
|
2021-04-14 10:30:52 +02:00
|
|
|
constructor(message: string, public readonly errors: unknown) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:04:22 +02:00
|
|
|
async function request(
|
|
|
|
method: HTTPMethod,
|
|
|
|
endpoint: string,
|
|
|
|
successStatus: number,
|
|
|
|
data: any,
|
|
|
|
authToken?: string
|
|
|
|
) {
|
|
|
|
const init = {
|
|
|
|
headers: new Headers(),
|
|
|
|
method: method,
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
};
|
|
|
|
const csrfToken = Cookies.get("csrftoken");
|
|
|
|
if (csrfToken != undefined) {
|
|
|
|
init.headers.set("X-CSRFToken", csrfToken);
|
2021-04-14 10:30:52 +02:00
|
|
|
}
|
2021-04-15 16:04:22 +02:00
|
|
|
if (authToken != undefined) {
|
2021-04-16 11:31:11 +02:00
|
|
|
init.headers.set("Authorization", `Token ${authToken}`);
|
2021-04-14 10:30:52 +02:00
|
|
|
}
|
2021-04-15 16:04:22 +02:00
|
|
|
init.headers.set("Accept", "application/json");
|
|
|
|
init.headers.set("Content-Type", "application/json");
|
2021-05-18 12:07:03 +02:00
|
|
|
const response = await fetch(`/${endpoint}/`, init);
|
2021-04-16 11:08:28 +02:00
|
|
|
if (response.status !== 204) {
|
|
|
|
if (response.status === successStatus) {
|
|
|
|
return await response.json();
|
|
|
|
} else {
|
|
|
|
throw new APIError(response.statusText, await response.json());
|
|
|
|
}
|
2021-04-15 14:24:55 +02:00
|
|
|
}
|
2021-04-14 10:30:52 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 12:07:03 +02:00
|
|
|
async function api_request(
|
|
|
|
method: HTTPMethod,
|
|
|
|
endpoint: string,
|
|
|
|
successStatus: number,
|
|
|
|
data: any,
|
|
|
|
authToken?: string
|
|
|
|
) {
|
|
|
|
return request(method, `api/${endpoint}`, successStatus, data, authToken);
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:04:22 +02:00
|
|
|
export class User {
|
2021-04-16 10:36:52 +02:00
|
|
|
email: string | undefined;
|
|
|
|
password: string | undefined;
|
2021-05-18 11:06:02 +02:00
|
|
|
username: string | null = null;
|
|
|
|
confidantEmail: string | null = null;
|
2021-04-16 10:36:52 +02:00
|
|
|
isAuthenticated = false;
|
2021-04-15 10:05:54 +02:00
|
|
|
private token = "";
|
|
|
|
|
2021-04-15 09:31:03 +02:00
|
|
|
static async confirm(uid: string, token: string): Promise<void> {
|
2021-05-18 12:07:03 +02:00
|
|
|
await api_request("POST", "users/activation", 204, { uid, token });
|
2021-04-15 09:31:03 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 11:21:39 +02:00
|
|
|
async login(): Promise<void> {
|
2021-05-18 12:07:03 +02:00
|
|
|
if (!this.email || !this.password) throw new APIError("", "");
|
|
|
|
|
|
|
|
// logout any existing sessions
|
|
|
|
//await logout()
|
|
|
|
// fetch the login endpoint we use for authentication
|
|
|
|
const loginEndpoint = "/api-auth/login/";
|
|
|
|
// fetch the login page, so it sets csrf cookies
|
|
|
|
await window.fetch(loginEndpoint);
|
|
|
|
|
|
|
|
// authenticate us
|
|
|
|
const body = new window.FormData();
|
|
|
|
body.append("username", this.email);
|
|
|
|
body.append("password", this.password);
|
|
|
|
const csrf_token = Cookies.get("csrftoken");
|
|
|
|
if (csrf_token) body.append("csrfmiddlewaretoken", csrf_token);
|
|
|
|
const res = await window.fetch(loginEndpoint, { method: "post", body });
|
|
|
|
|
|
|
|
// successful logins are followed by a redirect
|
|
|
|
if (res.redirected && res.status === 200) {
|
|
|
|
this.isAuthenticated = true;
|
|
|
|
} else {
|
|
|
|
throw new APIError("", "");
|
|
|
|
}
|
2021-04-14 11:21:39 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 14:24:55 +02:00
|
|
|
async save(): Promise<void> {
|
2021-05-18 12:07:03 +02:00
|
|
|
await api_request(
|
2021-04-15 16:04:22 +02:00
|
|
|
"PATCH",
|
|
|
|
"users/me",
|
|
|
|
200,
|
|
|
|
{
|
|
|
|
username: this.username,
|
2021-05-18 11:06:02 +02:00
|
|
|
confidant_email: this.confidantEmail,
|
2021-04-15 16:04:22 +02:00
|
|
|
},
|
|
|
|
this.token
|
|
|
|
);
|
2021-04-15 14:24:55 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 10:30:52 +02:00
|
|
|
async signup(): Promise<void> {
|
2021-05-18 12:07:03 +02:00
|
|
|
await api_request("POST", "users", 201, {
|
2021-04-15 16:04:22 +02:00
|
|
|
email: this.email,
|
|
|
|
password: this.password,
|
|
|
|
});
|
2021-04-14 10:30:52 +02:00
|
|
|
}
|
|
|
|
}
|