Skip to content

Commit

Permalink
Delete local data when logging out.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoBernardino committed Oct 5, 2021
1 parent 31ee24a commit 8328856
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion components/Panels/All.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const All = () => {
syncToken={syncToken}
db={db.current}
/>
<LogoutLink />
<LogoutLink db={db.current} />
</Wrapper>
);
};
Expand Down
9 changes: 9 additions & 0 deletions lib/data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,15 @@ export const deleteAllData = async (db: RxDatabase, syncToken: string) => {
await remoteDb.erase();
};

export const deleteLocalData = async (db: RxDatabase) => {
await db.events.remove();

// NOTE: The erase below doesn't work locally, so we need the line above
const localDb = new PouchDB(localDbName);
// @ts-ignore erase comes from pouchdb-erase
await localDb.erase();
};

type ExportAllData = (
db: RxDatabase,
) => Promise<{
Expand Down
14 changes: 11 additions & 3 deletions modules/auth/LogoutLink.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { useCallback, useState } from 'react';
import Swal from 'sweetalert2';
import styled from 'styled-components';
import { RxDatabase } from 'rxdb';

import Loading from 'components/Loading';
import Button from 'components/Button';
import { doLogout } from 'lib/utils';
import { deleteLocalData } from 'lib/data-utils';

interface LogoutLinkProps {
db: RxDatabase;
}

const Container = styled.div`
top: 8px;
right: 8px;
position: absolute;
`;

const LoginButton = () => {
const LogoutLink = ({ db }: LogoutLinkProps) => {
const [isSubmitting, setIsSubmitting] = useState(false);
const handleLogout = useCallback(() => {
const handleLogout = useCallback(async () => {
setIsSubmitting(true);

await deleteLocalData(db);

doLogout();

Swal.fire('Alright!', 'No idea who you are right now.', 'success');
Expand All @@ -34,4 +42,4 @@ const LoginButton = () => {
);
};

export default LoginButton;
export default LogoutLink;

0 comments on commit 8328856

Please sign in to comment.