Rework account storage system

This commit is contained in:
Zoe Roux 2023-12-01 18:34:11 +01:00
parent e0f41be887
commit fbf43fb10f
3 changed files with 110 additions and 82 deletions

View File

@ -0,0 +1,110 @@
/*
* Kyoo - A portable and vast media library solution.
* Copyright (c) Kyoo.
*
* See AUTHORS.md and LICENSE file in the project root for full license information.
*
* Kyoo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Kyoo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/
import { z } from "zod";
import { Account, AccountP } from "./accounts";
import { MMKV } from "react-native-mmkv";
const storage = new MMKV();
const readAccounts = () => {
const acc = storage.getString("accounts");
if (!acc) return [];
return z.array(AccountP).parse(JSON.parse(acc));
};
const writeAccounts = (accounts: Account[]) => {
storage.set("accounts", JSON.stringify(accounts));
};
export const setAccountCookie = (account?: Account) => {
const value = JSON.stringify(account);
// Remove illegal values from json. There should not be one in the account anyways.
value.replaceAll(";", "");
const d = new Date();
// A year
d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000);
const expires = value ? "expires=" + d.toUTCString() : "expires=Thu, 01 Jan 1970 00:00:01 GMT";
document.cookie = "account=" + value + ";" + expires + ";path=/";
return null;
}
export const readAccountCookie = (cookies?: string) => {
if (!cookies) return null;
const name = "account=";
const decodedCookie = decodeURIComponent(cookies);
const ca = decodedCookie.split(";");
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
const str = c.substring(name.length, c.length);
return AccountP.parse(JSON.parse(str));
}
}
return null;
}
export const getCurrentAccount = () => {
const accounts = readAccounts();
return accounts.find(x => x.selected);
}
export const addAccount = (account: Account) => {
const accounts = readAccounts();
// Prevent the user from adding the same account twice.
if (accounts.find(x => x.id == account.id)) {
updateAccount(account.id, account);
return;
}
for (const acc of accounts) acc.selected = false;
accounts.push(account);
writeAccounts(accounts);
};
export const removeAccounts = (filter: (acc: Account) => boolean) => {
let accounts = readAccounts();
accounts = accounts.filter(filter);
if (!accounts.find((x) => x.selected) && accounts.length > 0) {
accounts[0].selected = true;
}
writeAccounts(accounts);
};
export const updateAccount = (id: string, account: Account) => {
const accounts = readAccounts();
const idx = accounts.findIndex((x) => x.id == id);
if (idx === -1) return;
if (account.selected) {
for (const acc of accounts) acc.selected = false;
} else if(accounts[idx].selected) {
// we just unselected the current account, focus another one.
if (accounts.length > 0)
accounts[0].selected = true
}
accounts[idx] = account;
writeAccounts(accounts);
};

View File

@ -1,34 +0,0 @@
/*
* Kyoo - A portable and vast media library solution.
* Copyright (c) Kyoo.
*
* See AUTHORS.md and LICENSE file in the project root for full license information.
*
* Kyoo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Kyoo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/
import { MMKV } from "react-native-mmkv";
export const storage = new MMKV();
export const setSecureItem = (key: string, value: string | null) => {
if (value === null) storage.delete(key);
else storage.set(key, value);
};
export const deleteSecureItem = (key: string) => setSecureItem(key, null);
export const getSecureItem = (key: string, _cookies?: string): string | null => {
return storage.getString(key) || null;
};

View File

@ -1,48 +0,0 @@
/*
* Kyoo - A portable and vast media library solution.
* Copyright (c) Kyoo.
*
* See AUTHORS.md and LICENSE file in the project root for full license information.
*
* Kyoo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Kyoo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/
export const setSecureItem = (key: string, value: string | null) => {
const d = new Date();
// A year
d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000);
const expires = value ? "expires=" + d.toUTCString() : "expires=Thu, 01 Jan 1970 00:00:01 GMT";
document.cookie = key + "=" + value + ";" + expires + ";path=/";
return null;
};
export const deleteSecureItem = (key: string) => setSecureItem(key, null);
export const getSecureItem = (key: string, cookies?: string): string | null => {
// Don't try to use document's cookies on SSR.
if (!cookies && typeof window === "undefined") return null;
const name = key + "=";
const decodedCookie = decodeURIComponent(cookies ?? document.cookie);
const ca = decodedCookie.split(";");
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return null;
};