Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Implement logout

Hakan Shehu36d91c7d53f4
diff --git a/desktop/src/components/accounts/account-logout.tsx b/desktop/src/components/accounts/account-logout.tsx
new file mode 100644
index 00000000..23cc8d81
--- /dev/null
+++ b/desktop/src/components/accounts/account-logout.tsx
@@ -0,0 +1,55 @@
+import React from 'react';
+import {
+ AlertDialog,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+} from '@/components/ui/alert-dialog';
+import { Button } from '@/components/ui/button';
+import { Spinner } from '@/components/ui/spinner';
+
+interface AccountLogoutProps {
+ id: string;
+ onCancel: () => void;
+}
+
+export const AccountLogout = ({ id, onCancel }: AccountLogoutProps) => {
+ const [isDeleting, setIsDeleting] = React.useState(false);
+ return (
+ <AlertDialog
+ open={true}
+ onOpenChange={(open) => {
+ if (!open) {
+ onCancel();
+ }
+ }}
+ >
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>Are you sure you want logout?</AlertDialogTitle>
+ <AlertDialogDescription>
+ All your data will be removed from this device.
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel>Cancel</AlertDialogCancel>
+ <Button
+ variant="destructive"
+ disabled={isDeleting}
+ onClick={async () => {
+ setIsDeleting(true);
+ await window.globalDb.logout(id);
+ window.location.href = '/';
+ }}
+ >
+ {isDeleting && <Spinner className="mr-1" />}
+ Delete
+ </Button>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ );
+};
diff --git a/desktop/src/components/app.tsx b/desktop/src/components/app.tsx
index a607a89c..d5ceca5d 100644
--- a/desktop/src/components/app.tsx
+++ b/desktop/src/components/app.tsx
@@ -7,11 +7,13 @@ import { AxiosContext } from '@/contexts/axios';
import { useStore } from '@/contexts/store';
import { observer } from 'mobx-react-lite';
import { Outlet } from 'react-router-dom';
+import { AccountLogout } from '@/components/accounts/account-logout';
const serverUrl = 'http://localhost:3000';
export const App = observer(() => {
const store = useStore();
+ const [showLogout, setShowLogout] = React.useState(false);
React.useEffect(() => {
if (!store.loaded) {
@@ -46,10 +48,25 @@ export const App = observer(() => {
});
return (
- <AccountContext.Provider value={account}>
+ <AccountContext.Provider
+ value={{
+ ...account,
+ logout: () => {
+ setShowLogout(true);
+ },
+ }}
+ >
<AxiosContext.Provider value={axios}>
<Outlet />
</AxiosContext.Provider>
+ {showLogout && (
+ <AccountLogout
+ id={account.id}
+ onCancel={() => {
+ setShowLogout(false);
+ }}
+ />
+ )}
</AccountContext.Provider>
);
});
diff --git a/desktop/src/components/workspaces/sidebar-header.tsx b/desktop/src/components/workspaces/sidebar-header.tsx
index e791b5d1..ee62065c 100644
--- a/desktop/src/components/workspaces/sidebar-header.tsx
+++ b/desktop/src/components/workspaces/sidebar-header.tsx
@@ -9,13 +9,14 @@ import { Icon } from '@/components/ui/icon';
import { Avatar } from '@/components/ui/avatar';
import { useStore } from '@/contexts/store';
import { observer } from 'mobx-react-lite';
+import { useAccount } from '@/contexts/account';
export const SidebarHeader = observer(() => {
const store = useStore();
const workspace = useWorkspace();
+ const account = useAccount();
- const account = store.accounts[0];
- const workspaces = store.workspaces;
+ const allWorkspaces = store.workspaces;
return (
<Popover>
@@ -32,7 +33,7 @@ export const SidebarHeader = observer(() => {
<Icon name="expand-up-down-line" />
</div>
</PopoverTrigger>
- <PopoverContent align="start" className="flex w-96 flex-col gap-2 p-3">
+ <PopoverContent align="start" className="flex w-96 flex-col gap-2 p-2">
<h2 className="text-sm font-semibold">Account</h2>
<div className="flex flex-grow items-start gap-2">
<Avatar
@@ -48,7 +49,7 @@ export const SidebarHeader = observer(() => {
<hr className="-mx-1 my-1 h-px bg-muted" />
<h2 className="text-sm font-semibold">Workspaces</h2>
<ul className="flex flex-col gap-0.5">
- {workspaces.map((w) => {
+ {allWorkspaces.map((w) => {
return (
<li
key={w.id}
@@ -61,7 +62,7 @@ export const SidebarHeader = observer(() => {
})}
</ul>
<hr />
- <div className="flex flex-col gap-1">
+ <div className="flex flex-col">
<button className="flex flex-row items-center gap-2 rounded-md p-1 pl-0 text-sm outline-none hover:cursor-pointer hover:bg-gray-100">
<Icon name="add-line" />
<span>Create workspace</span>
@@ -70,7 +71,12 @@ export const SidebarHeader = observer(() => {
<Icon name="settings-4-line" />
<span>Settings</span>
</button>
- <button className="flex flex-row items-center gap-2 rounded-md p-1 pl-0 text-sm outline-none hover:cursor-pointer hover:bg-gray-100">
+ <button
+ className="flex flex-row items-center gap-2 rounded-md p-1 pl-0 text-sm outline-none hover:cursor-pointer hover:bg-gray-100"
+ onClick={() => {
+ account.logout();
+ }}
+ >
<Icon name="logout-circle-r-line" />
<span>Logout</span>
</button>
diff --git a/desktop/src/contexts/account.ts b/desktop/src/contexts/account.ts
index 3bd04635..a8c166d6 100644
--- a/desktop/src/contexts/account.ts
+++ b/desktop/src/contexts/account.ts
@@ -1,6 +1,12 @@
import { createContext, useContext } from 'react';
import { Account } from '@/types/accounts';
-export const AccountContext = createContext<Account>({} as Account);
+interface AccountContext extends Account {
+ logout: () => void;
+}
+
+export const AccountContext = createContext<AccountContext>(
+ {} as AccountContext,
+);
export const useAccount = () => useContext(AccountContext);
diff --git a/desktop/src/electron/database/global/index.ts b/desktop/src/electron/database/global/index.ts
index 4637c1af..2095e6f6 100644
--- a/desktop/src/electron/database/global/index.ts
+++ b/desktop/src/electron/database/global/index.ts
@@ -8,15 +8,17 @@ import { Transaction, TransactionType } from '@/types/transactions';
import { globalDatabaseMigrations } from '@/electron/database/global/migrations';
import { GlobalDatabaseData } from '@/types/global';
import { WorkspaceDatabase } from '@/electron/database/workspace';
+import * as fs from 'node:fs';
export class GlobalDatabase {
+ appPath: string;
database: Kysely<GlobalDatabaseSchema>;
workspaceDatabases: Map<string, WorkspaceDatabase> = new Map();
constructor() {
- const appPath = app.getPath('userData');
+ this.appPath = app.getPath('userData');
const dialect = new SqliteDialect({
- database: new SQLite(`${appPath}/global.db`),
+ database: new SQLite(`${this.appPath}/global.db`),
});
this.database = new Kysely<GlobalDatabaseSchema>({
@@ -36,7 +38,7 @@ export class GlobalDatabase {
return {
accounts,
- workspaces
+ workspaces,
};
};
@@ -68,15 +70,23 @@ export class GlobalDatabase {
return workspaceDatabase;
};
- initWorkspaceDatabase = async (
- workspace: Workspace,
- ): Promise<void> => {
+ initWorkspaceDatabase = async (workspace: Workspace): Promise<void> => {
const key = `${workspace.accountId}_${workspace.id}`;
+ const accountPath = this.buildAccountDirectoryPath(workspace.accountId);
+ if (!fs.existsSync(accountPath)) {
+ fs.mkdirSync(accountPath);
+ }
+
+ const workspaceDatabasePath = this.buildWorkspaceDatabasePath(
+ workspace.accountId,
+ workspace.id,
+ );
const workspaceDatabase = new WorkspaceDatabase(
workspace.accountId,
workspace.id,
workspace.userNodeId,
this,
+ workspaceDatabasePath,
);
await workspaceDatabase.migrate();
this.workspaceDatabases.set(key, workspaceDatabase);
@@ -112,6 +122,32 @@ export class GlobalDatabase {
.execute();
};
+ logout = async (accountId: string) => {
+ await this.database
+ .deleteFrom('accounts')
+ .where('id', '=', accountId)
+ .execute();
+
+ await this.database
+ .deleteFrom('workspaces')
+ .where('account_id', '=', accountId)
+ .execute();
+
+ await this.database
+ .deleteFrom('transactions')
+ .where('account_id', '=', accountId)
+ .execute();
+
+ const accountDirectoryPath = this.buildAccountDirectoryPath(accountId);
+ if (fs.existsSync(accountDirectoryPath)) {
+ fs.rm(accountDirectoryPath, { recursive: true, force: true }, (err) => {
+ if (err) {
+ console.error('Error deleting account directory: ', err);
+ }
+ });
+ }
+ };
+
getWorkspaces = async (): Promise<Workspace[]> => {
const workspaces = await this.database
.selectFrom('workspaces')
@@ -198,6 +234,14 @@ export class GlobalDatabase {
.where('id', '=', workspaceId)
.execute();
};
+
+ buildAccountDirectoryPath = (accountId: string) => {
+ return `${this.appPath}/account_${accountId}`;
+ };
+
+ buildWorkspaceDatabasePath = (accountId: string, workspaceId: string) => {
+ return `${this.buildAccountDirectoryPath(accountId)}/workspace_${workspaceId}.db`;
+ };
}
export const globalDatabase = new GlobalDatabase();
diff --git a/desktop/src/electron/database/workspace/index.ts b/desktop/src/electron/database/workspace/index.ts
index 4567c601..ad65faae 100644
--- a/desktop/src/electron/database/workspace/index.ts
+++ b/desktop/src/electron/database/workspace/index.ts
@@ -1,4 +1,3 @@
-import { app } from 'electron';
import SQLite from 'better-sqlite3';
import { Kysely, Migration, Migrator, SqliteDialect } from 'kysely';
import {
@@ -6,7 +5,6 @@ import {
WorkspaceDatabaseSchema,
} from '@/electron/database/workspace/schema';
import { workspaceDatabaseMigrations } from '@/electron/database/workspace/migrations';
-import * as fs from 'node:fs';
import { GlobalDatabase } from '@/electron/database/global';
import { CreateNodeInput, Node, UpdateNodeInput } from '@/types/nodes';
import { NeuronId } from '@/lib/id';
@@ -25,20 +23,15 @@ export class WorkspaceDatabase {
workspaceId: string,
userId: string,
globalDatabase: GlobalDatabase,
+ path: string,
) {
this.accountId = accountId;
this.workspaceId = workspaceId;
this.globalDatabase = globalDatabase;
this.userId = userId;
- const appPath = app.getPath('userData');
- const accountPath = `${appPath}/account_${accountId}`;
- if (!fs.existsSync(accountPath)) {
- fs.mkdirSync(accountPath);
- }
-
const dialect = new SqliteDialect({
- database: new SQLite(`${accountPath}/workspace_${workspaceId}.db`),
+ database: new SQLite(path),
});
this.database = new Kysely<WorkspaceDatabaseSchema>({
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index b54c343f..fbb370b7 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -78,6 +78,9 @@ ipcMain.handle('add-workspace', async (_, workspace) =>
ipcMain.handle('add-transaction', async (_, transaction) =>
globalDatabase.addTransaction(transaction),
);
+ipcMain.handle('logout', async (_, accountId) =>
+ globalDatabase.logout(accountId),
+);
ipcMain.handle('create-node', async (_, accountId, workspaceId, input) => {
const workspaceDb = globalDatabase.getWorkspaceDatabase(
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index de1ae0e1..be242dc9 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -14,6 +14,7 @@ contextBridge.exposeInMainWorld('globalDb', {
ipcRenderer.invoke('add-workspace', workspace),
addTransaction: (transaction: Transaction) =>
ipcRenderer.invoke('add-transaction', transaction),
+ logout: (accountId: string) => ipcRenderer.invoke('logout', accountId),
});
contextBridge.exposeInMainWorld('workspaceDb', {
diff --git a/desktop/src/types/window.d.ts b/desktop/src/types/window.d.ts
index 4330f631..8c4341ef 100644
--- a/desktop/src/types/window.d.ts
+++ b/desktop/src/types/window.d.ts
@@ -12,6 +12,7 @@ export interface GlobalDbApi {
getWorkspaces: () => Promise<Workspace[]>;
addWorkspace: (workspace: Workspace) => Promise<void>;
addTransaction: (transaction: Transaction) => Promise<void>;
+ logout: (accountId: string) => Promise<void>;
}
export interface WorkspaceDbApi {