2022-03-03 22:52:07 +01:00
|
|
|
import db from "./db";
|
2022-12-27 03:27:07 +01:00
|
|
|
import session from "./Session";
|
2022-03-03 22:52:07 +01:00
|
|
|
|
|
|
|
class UserManager {
|
|
|
|
async all() {
|
2022-12-27 03:27:07 +01:00
|
|
|
const users = await db.users.toArray();
|
|
|
|
if (session.exists()) {
|
|
|
|
users.unshift(this.localUser());
|
|
|
|
}
|
|
|
|
return users;
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(baseUrl) {
|
2023-01-05 04:47:12 +01:00
|
|
|
if (session.exists() && baseUrl === config.base_url) {
|
2022-12-27 03:27:07 +01:00
|
|
|
return this.localUser();
|
|
|
|
}
|
2022-03-03 22:52:07 +01:00
|
|
|
return db.users.get(baseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
async save(user) {
|
2023-01-05 04:47:12 +01:00
|
|
|
if (session.exists() && user.baseUrl === config.base_url) {
|
2022-12-27 03:27:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-03-03 22:52:07 +01:00
|
|
|
await db.users.put(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(baseUrl) {
|
2023-01-05 04:47:12 +01:00
|
|
|
if (session.exists() && baseUrl === config.base_url) {
|
2022-12-27 03:27:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-03-03 22:52:07 +01:00
|
|
|
await db.users.delete(baseUrl);
|
|
|
|
}
|
2022-12-27 03:27:07 +01:00
|
|
|
|
|
|
|
localUser() {
|
|
|
|
if (!session.exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
2023-01-05 04:47:12 +01:00
|
|
|
baseUrl: config.base_url,
|
2022-12-27 03:27:07 +01:00
|
|
|
username: session.username(),
|
|
|
|
token: session.token() // Not "password"!
|
|
|
|
};
|
|
|
|
}
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const userManager = new UserManager();
|
|
|
|
export default userManager;
|