diff --git a/front/packages/models/src/login.ts b/front/packages/models/src/login.ts index b7152439..eef923b1 100644 --- a/front/packages/models/src/login.ts +++ b/front/packages/models/src/login.ts @@ -96,5 +96,20 @@ export const getToken = async (cookies?: string): Promise => (await getTokenWJ(cookies))[0] 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(); } diff --git a/front/packages/models/src/query.tsx b/front/packages/models/src/query.tsx index 81bd345b..0d0c7a97 100644 --- a/front/packages/models/src/query.tsx +++ b/front/packages/models/src/query.tsx @@ -49,7 +49,7 @@ export const queryFn = async ( | { path: (string | false | undefined | null)[]; body?: object; - method: "GET" | "POST"; + method: "GET" | "POST" | "DELETE"; authenticated?: boolean; apiUrl?: string }, @@ -100,10 +100,14 @@ export const queryFn = async ( } catch (e) { 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; } + // If the method is DELETE, 204 NoContent is returned from kyoo. + // @ts-ignore + if (context.method === "DELETE") return undefined; + let data; try { data = await resp.json(); diff --git a/front/packages/ui/src/navbar/index.tsx b/front/packages/ui/src/navbar/index.tsx index fd659729..f288f45a 100644 --- a/front/packages/ui/src/navbar/index.tsx +++ b/front/packages/ui/src/navbar/index.tsx @@ -18,8 +18,19 @@ * along with Kyoo. If not, see . */ -import { Library, LibraryP, logout, Page, Paged, QueryIdentifier, User, UserP } from "@kyoo/models"; import { + deleteAccount, + Library, + LibraryP, + logout, + Page, + Paged, + QueryIdentifier, + User, + UserP, +} from "@kyoo/models"; +import { + Alert, Input, IconButton, Header, @@ -116,11 +127,36 @@ export const NavbarProfile = () => { <> { - logout(); + onSelect={async () => { + await logout(); queryClient.invalidateQueries(["auth", "me"]); }} /> + { + 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", + }, + ); + }} + /> )} @@ -157,9 +193,9 @@ export const NavbarRight = () => { onPress={ Platform.OS === "web" ? () => { - setSearch(true); - setTimeout(() => ref.current?.focus(), 0); - } + setSearch(true); + setTimeout(() => ref.current?.focus(), 0); + } : () => push("/search") } {...tooltip(t("navbar.search"))} diff --git a/front/translations/en.json b/front/translations/en.json index 85e21e5b..dac76964 100644 --- a/front/translations/en.json +++ b/front/translations/en.json @@ -28,7 +28,9 @@ }, "misc": { "prev-page": "Previous page", - "next-page": "Next page" + "next-page": "Next page", + "delete": "Delete", + "cancel": "Cancel" }, "navbar": { "home": "Home", @@ -66,7 +68,9 @@ "confirm": "Confirm Password", "or-register": "Don’t have an account? <1>Register.", "or-login": "Have an account already? <1>Log in.", - "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": { "connection": "Could not connect to the kyoo's server", diff --git a/front/translations/fr.json b/front/translations/fr.json index cf044a77..2310108a 100644 --- a/front/translations/fr.json +++ b/front/translations/fr.json @@ -28,7 +28,9 @@ }, "misc": { "prev-page": "Page précédente", - "next-page": "Page suivante" + "next-page": "Page suivante", + "delete": "Supprimer", + "cancel": "Annuler" }, "navbar": { "home": "Accueil", @@ -66,7 +68,9 @@ "confirm": "Confirm Password", "or-register": "Vous n'avez pas de compte ? <1>Inscrivez-vous.", "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": { "connection": "Impossible de se connecter au serveur de kyoo.",