Add a delete account button

This commit is contained in:
Zoe Roux 2023-06-14 16:54:10 +09:00
parent 7cffd75749
commit 663de0e720
5 changed files with 76 additions and 13 deletions

View File

@ -96,5 +96,20 @@ 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 () =>{
deleteSecureItem("auth") 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);
await setSecureItem("accounts", JSON.stringify(accounts));
}
await deleteSecureItem("auth")
}
export const deleteAccount = async () => {
await queryFn({ path: ["auth", "me"], method: "DELETE"});
await logout();
} }

View File

@ -49,7 +49,7 @@ export const queryFn = async <Data,>(
| { | {
path: (string | false | undefined | null)[]; path: (string | false | undefined | null)[];
body?: object; body?: object;
method: "GET" | "POST"; method: "GET" | "POST" | "DELETE";
authenticated?: boolean; authenticated?: boolean;
apiUrl?: string apiUrl?: string
}, },
@ -100,10 +100,14 @@ export const queryFn = async <Data,>(
} catch (e) { } catch (e) {
data = { errors: [error] } as KyooErrors; data = { errors: [error] } as KyooErrors;
} }
console.log(`Invalid response (${path}):`, data, resp.status); console.log(`Invalid response (${"method" in context && context.method ? context.method : "GET"} ${path}):`, data, resp.status);
throw data as KyooErrors; throw data as KyooErrors;
} }
// If the method is DELETE, 204 NoContent is returned from kyoo.
// @ts-ignore
if (context.method === "DELETE") return undefined;
let data; let data;
try { try {
data = await resp.json(); data = await resp.json();

View File

@ -18,8 +18,19 @@
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>. * along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Library, LibraryP, logout, Page, Paged, QueryIdentifier, User, UserP } from "@kyoo/models";
import { import {
deleteAccount,
Library,
LibraryP,
logout,
Page,
Paged,
QueryIdentifier,
User,
UserP,
} from "@kyoo/models";
import {
Alert,
Input, Input,
IconButton, IconButton,
Header, Header,
@ -116,11 +127,36 @@ export const NavbarProfile = () => {
<> <>
<Menu.Item <Menu.Item
label={t("login.logout")} label={t("login.logout")}
onSelect={() => { onSelect={async () => {
logout(); await logout();
queryClient.invalidateQueries(["auth", "me"]); queryClient.invalidateQueries(["auth", "me"]);
}} }}
/> />
<Menu.Item
label={t("login.delete")}
onSelect={async () => {
Alert.alert(
t("login.delete"),
t("login.delete-confirmation"),
[
{
text: t("misc.delete"),
onPress: async () => {
await deleteAccount();
queryClient.invalidateQueries(["auth", "me"]);
},
style: "destructive",
},
{ text: t("misc.cancel"), style: "cancel" },
],
{
cancelable: true,
userInterfaceStyle: theme.mode === "auto" ? "light" : theme.mode,
icon: "warning",
},
);
}}
/>
</> </>
)} )}
</Menu> </Menu>
@ -157,9 +193,9 @@ export const NavbarRight = () => {
onPress={ onPress={
Platform.OS === "web" Platform.OS === "web"
? () => { ? () => {
setSearch(true); setSearch(true);
setTimeout(() => ref.current?.focus(), 0); setTimeout(() => ref.current?.focus(), 0);
} }
: () => push("/search") : () => push("/search")
} }
{...tooltip(t("navbar.search"))} {...tooltip(t("navbar.search"))}

View File

@ -28,7 +28,9 @@
}, },
"misc": { "misc": {
"prev-page": "Previous page", "prev-page": "Previous page",
"next-page": "Next page" "next-page": "Next page",
"delete": "Delete",
"cancel": "Cancel"
}, },
"navbar": { "navbar": {
"home": "Home", "home": "Home",
@ -66,7 +68,9 @@
"confirm": "Confirm Password", "confirm": "Confirm Password",
"or-register": "Dont have an account? <1>Register</1>.", "or-register": "Dont have an account? <1>Register</1>.",
"or-login": "Have an account already? <1>Log in</1>.", "or-login": "Have an account already? <1>Log in</1>.",
"password-no-match": "Passwords do not match." "password-no-match": "Passwords do not match.",
"delete": "Delete your account",
"delete-confirmation": "This action can't be reverted. Are you sure?"
}, },
"errors": { "errors": {
"connection": "Could not connect to the kyoo's server", "connection": "Could not connect to the kyoo's server",

View File

@ -28,7 +28,9 @@
}, },
"misc": { "misc": {
"prev-page": "Page précédente", "prev-page": "Page précédente",
"next-page": "Page suivante" "next-page": "Page suivante",
"delete": "Supprimer",
"cancel": "Annuler"
}, },
"navbar": { "navbar": {
"home": "Accueil", "home": "Accueil",
@ -66,7 +68,9 @@
"confirm": "Confirm Password", "confirm": "Confirm Password",
"or-register": "Vous n'avez pas de compte ? <1>Inscrivez-vous</1>.", "or-register": "Vous n'avez pas de compte ? <1>Inscrivez-vous</1>.",
"or-login": "Vous avez déjà un compte ? <1>Connectez-vous.<1/>", "or-login": "Vous avez déjà un compte ? <1>Connectez-vous.<1/>",
"password-no-match": "Mots de passe differents" "password-no-match": "Mots de passe differents",
"delete": "Supprimer votre compte",
"delete-confirmation": "Cette action ne peut pas être annulée. Êtes-vous sur?"
}, },
"errors": { "errors": {
"connection": "Impossible de se connecter au serveur de kyoo.", "connection": "Impossible de se connecter au serveur de kyoo.",