wip: Replace the secure store

This commit is contained in:
Zoe Roux
2023-07-13 23:23:10 +09:00
parent fee59833f2
commit 1237a9157c
7 changed files with 64 additions and 46 deletions
+28 -30
View File
@@ -42,33 +42,31 @@ type Result<A, B> =
export type Account = Token & { apiUrl: string; username: string };
export const useAccounts = () => {
const [accounts, setAccounts] = useState<Account[]>([]);
const [selected, setSelected] = useState(0);
const [accounts] = useState<Account[]>(JSON.parse(getSecureItem("accounts") ?? "[]"));
const [selected, setSelected] = useState<number>(parseInt(getSecureItem("selected") ?? "0"));
useEffect(() => {
async function run() {
setAccounts(JSON.parse(await getSecureItem("accounts") ?? "[]"));
}
return {
accounts,
selected,
setSelected: (selected: number) => {
setSelected(selected);
setSecureItem("selected", selected.toString());
},
};
};
run();
}, []);
return {accounts, selected, setSelected};
}
const addAccount = async (token: Token, apiUrl: string, username: string | null): Promise<void> => {
const accounts: Account[] = JSON.parse(await getSecureItem("accounts") ?? "[]");
const accIdx = accounts.findIndex(x => x.refresh_token === token.refresh_token);
if (accIdx === -1)
accounts.push({...token, username, apiUrl});
else
accounts[accIdx] = {...accounts[accIdx], ...token};
await setSecureItem("accounts", JSON.stringify(accounts));
}
const addAccount = (token: Token, apiUrl: string, username: string | null) => {
const accounts: Account[] = JSON.parse(getSecureItem("accounts") ?? "[]");
const accIdx = accounts.findIndex((x) => x.refresh_token === token.refresh_token);
if (accIdx === -1) accounts.push({ ...token, username: username!, apiUrl });
else accounts[accIdx] = { ...accounts[accIdx], ...token };
setSecureItem("accounts", JSON.stringify(accounts));
};
export const loginFunc = async (
action: "register" | "login" | "refresh",
body: { username: string, password: string, email?: string } | string,
apiUrl?: string
body: { username: string; password: string; email?: string } | string,
apiUrl?: string,
): Promise<Result<Token, string>> => {
try {
const token = await queryFn(
@@ -107,23 +105,23 @@ export const getTokenWJ = async (cookies?: string): Promise<[string, Token] | [n
};
export const getToken = async (cookies?: string): Promise<string | null> =>
(await getTokenWJ(cookies))[0]
(await getTokenWJ(cookies))[0];
export const logout = async () =>{
export const logout = async () => {
if (Platform.OS !== "web") {
const tokenStr = await getSecureItem("auth");
if (!tokenStr) return;
const token = TokenP.parse(JSON.parse(tokenStr));
let accounts: Account[] = JSON.parse(await getSecureItem("accounts") ?? "[]");
accounts = accounts.filter(x => x.refresh_token !== token.refresh_token);
let accounts: Account[] = JSON.parse((await getSecureItem("accounts")) ?? "[]");
accounts = accounts.filter((x) => x.refresh_token !== token.refresh_token);
await setSecureItem("accounts", JSON.stringify(accounts));
}
await deleteSecureItem("auth")
}
await deleteSecureItem("auth");
};
export const deleteAccount = async () => {
await queryFn({ path: ["auth", "me"], method: "DELETE"});
await queryFn({ path: ["auth", "me"], method: "DELETE" });
await logout();
}
};
+16 -5
View File
@@ -18,8 +18,19 @@
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/
export {
setItemAsync as setSecureItem,
deleteItemAsync as deleteSecureItem,
getItemAsync as getSecureItem,
} from "expo-secure-store";
import { MMKV } from 'react-native-mmkv'
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;
}
@@ -18,7 +18,7 @@
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/
export const setSecureItemSync = (key: string, value: string | null) => {
export const setSecureItem = (key: string, value: string | null) => {
const d = new Date();
// A year
d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000);
@@ -27,12 +27,9 @@ export const setSecureItemSync = (key: string, value: string | null) => {
return null;
};
export const setSecureItem = async (key: string, value: string | null): Promise<null> =>
setSecureItemSync(key, value);
export const deleteSecureItem = (key: string) => setSecureItem(key, null);
export const deleteSecureItem = async (key: string) => setSecureItem(key, null);
export const getSecureItem = async (key: string, cookies?: string): Promise<string | 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 + "=";