web-base/admin/src/api.js

40 lines
1004 B
JavaScript
Raw Normal View History

2020-06-14 12:38:35 +02:00
import 'babel-polyfill';
export default class API {
constructor() {
2020-06-14 22:35:01 +02:00
this.loggedIn = false;
this.user = { };
}
csrfToken() {
return this.loggedIn ? this.user.session.csrf_token : null;
}
async apiCall(method, params) {
params = params || { };
params.csrf_token = this.csrfToken();
let response = await fetch("/api/" + method, {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(params)
});
return await response.json();
2020-06-14 12:38:35 +02:00
}
async fetchUser() {
2020-06-14 22:35:01 +02:00
let response = await fetch("/api/user/info");
let data = await response.json();
this.user = data["user"];
this.loggedIn = data["loggedIn"];
return data && data.success && data.loggedIn;
2020-06-14 12:38:35 +02:00
}
async logout() {
2020-06-14 22:35:01 +02:00
return this.apiCall("user/logout");
2020-06-14 12:38:35 +02:00
}
2020-06-15 00:00:15 +02:00
async getNotifications() {
return this.apiCall("notifications/fetch");
}
2020-06-14 12:38:35 +02:00
};