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]
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)[];
body?: object;
method: "GET" | "POST";
method: "GET" | "POST" | "DELETE";
authenticated?: boolean;
apiUrl?: string
},
@ -100,10 +100,14 @@ export const queryFn = async <Data,>(
} 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();

View File

@ -18,8 +18,19 @@
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/
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 = () => {
<>
<Menu.Item
label={t("login.logout")}
onSelect={() => {
logout();
onSelect={async () => {
await logout();
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>
@ -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"))}

View File

@ -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": "Dont have an account? <1>Register</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": {
"connection": "Could not connect to the kyoo's server",

View File

@ -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</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": {
"connection": "Impossible de se connecter au serveur de kyoo.",