Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

feat: allow revoking login devices

tianma-ifeabae678e885
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index f89a8c49..9942ec8b 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -528,6 +528,58 @@ app.get("/api/v1/auth/sessions", async (c) => {
});
});
+app.delete("/api/v1/auth/sessions", async (c) => {
+ const auth = await authenticateRequest(c, true);
+
+ if (!auth || auth.kind !== "user" || !auth.actorId || !auth.sessionId) {
+ return unauthorized(c, "An interactive user session is required.");
+ }
+
+ const now = isoNow();
+ await c.env.DB.batch([
+ c.env.DB.prepare(
+ `UPDATE sessions
+ SET revoked_at = ?
+ WHERE user_id = ? AND id != ? AND revoked_at IS NULL AND expires_at > ?`
+ ).bind(now, auth.actorId, auth.sessionId, now),
+ auditStatement(c.env.DB, "user", auth.actorId, "auth.sessions_revoke_others", "session", auth.sessionId, {}),
+ ]);
+
+ return c.json({ ok: true });
+});
+
+app.delete("/api/v1/auth/sessions/:sessionId", async (c) => {
+ const auth = await authenticateRequest(c, true);
+
+ if (!auth || auth.kind !== "user" || !auth.actorId || !auth.sessionId) {
+ return unauthorized(c, "An interactive user session is required.");
+ }
+
+ const sessionId = c.req.param("sessionId");
+ if (sessionId === auth.sessionId) {
+ return apiError(c, "current_session_cannot_be_revoked", "The current session cannot be revoked here.", 400);
+ }
+
+ const now = isoNow();
+ const session = await c.env.DB.prepare(
+ `SELECT id FROM sessions
+ WHERE id = ? AND user_id = ? AND revoked_at IS NULL AND expires_at > ?`
+ )
+ .bind(sessionId, auth.actorId, now)
+ .first<{ id: string }>();
+
+ if (!session) {
+ return notFound(c, "Login session not found.");
+ }
+
+ await c.env.DB.batch([
+ c.env.DB.prepare(`UPDATE sessions SET revoked_at = ? WHERE id = ? AND revoked_at IS NULL`).bind(now, session.id),
+ auditStatement(c.env.DB, "user", auth.actorId, "auth.session_revoke", "session", session.id, {}),
+ ]);
+
+ return c.json({ ok: true });
+});
+
app.post("/api/v1/auth/login", zValidator("json", LoginSchema), async (c) => {
const authMode = await getInstanceAuthMode(c.env);
if (authMode === "unconfigured") {
diff --git a/apps/web/src/components/SettingsPane.tsx b/apps/web/src/components/SettingsPane.tsx
index b81bc85b..9b0b9223 100644
--- a/apps/web/src/components/SettingsPane.tsx
+++ b/apps/web/src/components/SettingsPane.tsx
@@ -201,7 +201,7 @@ export const SettingsPane = ({
return (
<SettingsGroup>
<PasswordCard authRequired={authRequired} demoMode={demoMode} />
- <LoginDevicesCard authRequired={authRequired} />
+ {demoMode ? null : <LoginDevicesCard authRequired={authRequired} />}
<SessionCard authRequired={authRequired} isLoggingOut={isLoggingOut} onLogout={onLogout} />
</SettingsGroup>
);
diff --git a/apps/web/src/components/settings/LoginDevicesCard.tsx b/apps/web/src/components/settings/LoginDevicesCard.tsx
index c0e25140..3096bb68 100644
--- a/apps/web/src/components/settings/LoginDevicesCard.tsx
+++ b/apps/web/src/components/settings/LoginDevicesCard.tsx
@@ -1,8 +1,11 @@
-import { useQuery } from "@tanstack/react-query";
+import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Laptop, MonitorSmartphone, Smartphone, Tablet } from "lucide-react";
+import { useState } from "react";
import { useTranslation } from "react-i18next";
+import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { api } from "@/lib/api";
+import { LoginSessionRevokeDialog, type LoginSessionRevokeTarget } from "./LoginSessionRevokeDialog";
interface LoginDevicesCardProps {
authRequired: boolean;
@@ -64,62 +67,102 @@ const formatSessionTime = (value: string, locale: string) =>
export const LoginDevicesCard = ({ authRequired }: LoginDevicesCardProps) => {
const { t, i18n } = useTranslation();
+ const queryClient = useQueryClient();
+ const [revokeTarget, setRevokeTarget] = useState<LoginSessionRevokeTarget | null>(null);
const locale = i18n.resolvedLanguage ?? i18n.language;
const sessionsQuery = useQuery({
queryKey: ["auth", "sessions"],
queryFn: api.listLoginDeviceSessions,
enabled: authRequired,
});
+ const revokeMutation = useMutation({
+ mutationFn: (target: LoginSessionRevokeTarget) =>
+ target.type === "others"
+ ? api.revokeOtherLoginDeviceSessions()
+ : api.revokeLoginDeviceSession(target.sessionId),
+ onSuccess: async () => {
+ setRevokeTarget(null);
+ await queryClient.invalidateQueries({ queryKey: ["auth", "sessions"] });
+ },
+ });
if (!authRequired) return null;
+ const handleDialogOpenChange = (open: boolean) => {
+ if (!open && !revokeMutation.isPending) {
+ setRevokeTarget(null);
+ revokeMutation.reset();
+ }
+ };
+
return (
- <Card className="w-full min-w-0 overflow-hidden shadow-none">
- <CardHeader className="p-4">
- <CardTitle className="flex items-center gap-2 text-sm">
- <MonitorSmartphone className="h-4 w-4 text-emerald-700" />
- {t("loginDevices.title")}
- </CardTitle>
- <CardDescription>{t("loginDevices.description")}</CardDescription>
- </CardHeader>
- <CardContent className="p-4 pt-0">
- {sessionsQuery.isLoading ? <p className="text-sm text-slate-500">{t("loginDevices.loading")}</p> : null}
- {sessionsQuery.isError ? <p className="text-sm font-medium text-rose-600" role="alert">{t("loginDevices.loadFailed")}</p> : null}
- {sessionsQuery.data?.sessions.length === 0 ? <p className="text-sm text-slate-500">{t("loginDevices.empty")}</p> : null}
- {sessionsQuery.data?.sessions.length ? (
- <ul className="divide-y divide-slate-100">
- {sessionsQuery.data.sessions.map((session) => {
- const { deviceKind, os, browser } = describeUserAgent(session.userAgent);
- const DeviceIcon = getDeviceIcon(deviceKind);
- const details = [os, browser || t("loginDevices.unknownBrowser")].filter(Boolean).join(" · ");
+ <>
+ <Card className="w-full min-w-0 overflow-hidden shadow-none">
+ <CardHeader className="p-4">
+ <div>
+ <CardTitle className="flex items-center gap-2 text-sm">
+ <MonitorSmartphone className="h-4 w-4 text-emerald-700" />
+ {t("loginDevices.title")}
+ </CardTitle>
+ <CardDescription className="mt-1">{t("loginDevices.description")}</CardDescription>
+ </div>
+ </CardHeader>
+ <CardContent className="p-4 pt-0">
+ {sessionsQuery.isLoading ? <p className="text-sm text-slate-500">{t("loginDevices.loading")}</p> : null}
+ {sessionsQuery.isError ? <p className="text-sm font-medium text-rose-600" role="alert">{t("loginDevices.loadFailed")}</p> : null}
+ {sessionsQuery.data?.sessions.length === 0 ? <p className="text-sm text-slate-500">{t("loginDevices.empty")}</p> : null}
+ {sessionsQuery.data?.sessions.length ? (
+ <ul className="divide-y divide-slate-100">
+ {sessionsQuery.data.sessions.map((session) => {
+ const { deviceKind, os, browser } = describeUserAgent(session.userAgent);
+ const DeviceIcon = getDeviceIcon(deviceKind);
+ const details = [os, browser || t("loginDevices.unknownBrowser")].filter(Boolean).join(" · ");
- return (
- <li key={session.id} className="flex gap-3 py-3 first:pt-0 last:pb-0">
- <span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-slate-100 text-slate-600">
- <DeviceIcon className="h-4 w-4" aria-hidden="true" />
- </span>
- <div className="min-w-0 flex-1">
- <div className="flex flex-wrap items-center gap-2">
- <p className="text-sm font-semibold text-slate-800">{t(`loginDevices.${deviceKind}`)}</p>
- {session.isCurrent ? (
- <span className="rounded-full bg-emerald-50 px-2 py-0.5 text-[11px] font-semibold text-emerald-700">
- {t("loginDevices.current")}
- </span>
- ) : null}
+ return (
+ <li key={session.id} className="flex gap-3 py-3 first:pt-0 last:pb-0">
+ <span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-slate-100 text-slate-600">
+ <DeviceIcon className="h-4 w-4" aria-hidden="true" />
+ </span>
+ <div className="min-w-0 flex-1">
+ <div className="flex flex-wrap items-center gap-2">
+ <p className="text-sm font-semibold text-slate-800">{t(`loginDevices.${deviceKind}`)}</p>
+ {session.isCurrent ? (
+ <span className="rounded-full bg-emerald-50 px-2 py-0.5 text-[11px] font-semibold text-emerald-700">
+ {t("loginDevices.current")}
+ </span>
+ ) : null}
+ </div>
+ <p className="mt-0.5 truncate text-xs text-slate-500" title={session.userAgent ?? undefined}>{details}</p>
+ <p className="mt-1 text-xs text-slate-500">
+ {t("loginDevices.lastSeen", { time: formatSessionTime(session.lastSeenAt, locale) })}
+ <span aria-hidden="true"> · </span>
+ {t("loginDevices.signedIn", { time: formatSessionTime(session.createdAt, locale) })}
+ </p>
</div>
- <p className="mt-0.5 truncate text-xs text-slate-500" title={session.userAgent ?? undefined}>{details}</p>
- <p className="mt-1 text-xs text-slate-500">
- {t("loginDevices.lastSeen", { time: formatSessionTime(session.lastSeenAt, locale) })}
- <span aria-hidden="true"> · </span>
- {t("loginDevices.signedIn", { time: formatSessionTime(session.createdAt, locale) })}
- </p>
- </div>
- </li>
- );
- })}
- </ul>
- ) : null}
- </CardContent>
- </Card>
+ {!session.isCurrent ? (
+ <Button
+ size="sm"
+ variant="danger"
+ disabled={revokeMutation.isPending}
+ onClick={() => setRevokeTarget({ type: "session", sessionId: session.id })}
+ >
+ {t("loginDevices.revokeDevice")}
+ </Button>
+ ) : null}
+ </li>
+ );
+ })}
+ </ul>
+ ) : null}
+ </CardContent>
+ </Card>
+ <LoginSessionRevokeDialog
+ target={revokeTarget}
+ isPending={revokeMutation.isPending}
+ isError={revokeMutation.isError}
+ onOpenChange={handleDialogOpenChange}
+ onConfirm={() => revokeTarget && revokeMutation.mutate(revokeTarget)}
+ />
+ </>
);
};
diff --git a/apps/web/src/components/settings/LoginSessionRevokeDialog.tsx b/apps/web/src/components/settings/LoginSessionRevokeDialog.tsx
new file mode 100644
index 00000000..293f298a
--- /dev/null
+++ b/apps/web/src/components/settings/LoginSessionRevokeDialog.tsx
@@ -0,0 +1,58 @@
+import { useTranslation } from "react-i18next";
+import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogClose,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
+
+export type LoginSessionRevokeTarget =
+ | { type: "session"; sessionId: string }
+ | { type: "others" };
+
+interface LoginSessionRevokeDialogProps {
+ target: LoginSessionRevokeTarget | null;
+ isPending: boolean;
+ isError: boolean;
+ onOpenChange: (open: boolean) => void;
+ onConfirm: () => void;
+}
+
+export const LoginSessionRevokeDialog = ({
+ target,
+ isPending,
+ isError,
+ onOpenChange,
+ onConfirm,
+}: LoginSessionRevokeDialogProps) => {
+ const { t } = useTranslation();
+ const revokeOthers = target?.type === "others";
+
+ return (
+ <Dialog open={target !== null} onOpenChange={onOpenChange}>
+ <DialogContent className="max-w-md">
+ <DialogHeader>
+ <DialogTitle>
+ {t(revokeOthers ? "loginDevices.revokeOthersTitle" : "loginDevices.revokeDeviceTitle")}
+ </DialogTitle>
+ <DialogDescription>
+ {t(revokeOthers ? "loginDevices.revokeOthersDescription" : "loginDevices.revokeDeviceDescription")}
+ </DialogDescription>
+ </DialogHeader>
+ {isError ? <p className="text-sm font-medium text-rose-600" role="alert">{t("loginDevices.revokeFailed")}</p> : null}
+ <DialogFooter>
+ <DialogClose asChild>
+ <Button variant="outline" disabled={isPending}>{t("common.cancel")}</Button>
+ </DialogClose>
+ <Button variant="danger" disabled={isPending} onClick={onConfirm}>
+ {isPending ? t("loginDevices.revoking") : t("loginDevices.revokeConfirm")}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+};
diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts
index 282e8c7a..928cef3a 100644
--- a/apps/web/src/lib/api.ts
+++ b/apps/web/src/lib/api.ts
@@ -124,6 +124,12 @@ export const api = {
listLoginDeviceSessions: () =>
request<ListLoginDeviceSessionsResponse>("/api/v1/auth/sessions"),
+ revokeLoginDeviceSession: (sessionId: string) =>
+ request<{ ok: true }>(`/api/v1/auth/sessions/${sessionId}`, { method: "DELETE" }),
+
+ revokeOtherLoginDeviceSessions: () =>
+ request<{ ok: true }>("/api/v1/auth/sessions", { method: "DELETE" }),
+
login: (payload: { username: string; password: string }) =>
request<AuthSession>("/api/v1/auth/login", {
method: "POST",
diff --git a/docs/openapi.json b/docs/openapi.json
index 218824d1..916bbbed 100644
--- a/docs/openapi.json
+++ b/docs/openapi.json
@@ -113,6 +113,54 @@
"$ref": "#/components/responses/Error"
}
}
+ },
+ "delete": {
+ "summary": "Revoke all login sessions except the current session",
+ "responses": {
+ "200": {
+ "description": "Other sessions revoked",
+ "content": {
+ "application/json": {
+ "schema": { "$ref": "#/components/schemas/Ok" }
+ }
+ }
+ },
+ "401": {
+ "$ref": "#/components/responses/Error"
+ }
+ }
+ }
+ },
+ "/api/v1/auth/sessions/{sessionId}": {
+ "delete": {
+ "summary": "Revoke one of the current user's other login sessions",
+ "parameters": [
+ {
+ "name": "sessionId",
+ "in": "path",
+ "required": true,
+ "schema": { "type": "string" }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "Session revoked",
+ "content": {
+ "application/json": {
+ "schema": { "$ref": "#/components/schemas/Ok" }
+ }
+ }
+ },
+ "400": {
+ "$ref": "#/components/responses/Error"
+ },
+ "401": {
+ "$ref": "#/components/responses/Error"
+ },
+ "404": {
+ "$ref": "#/components/responses/Error"
+ }
+ }
}
},
"/api/v1/auth/login": {
diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts
index be234e3a..52003b74 100644
--- a/packages/client/src/index.ts
+++ b/packages/client/src/index.ts
@@ -175,6 +175,12 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {
listLoginDeviceSessions: () =>
request<ListLoginDeviceSessionsResponse>("/api/v1/auth/sessions"),
+ revokeLoginDeviceSession: (sessionId: string) =>
+ request<{ ok: true }>(`/api/v1/auth/sessions/${sessionId}`, { method: "DELETE" }),
+
+ revokeOtherLoginDeviceSessions: () =>
+ request<{ ok: true }>("/api/v1/auth/sessions", { method: "DELETE" }),
+
login: (payload: { username: string; password: string }) =>
request<AuthSession>("/api/v1/auth/login", {
method: "POST",
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index ecef92f1..3f969619 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -591,6 +591,15 @@ export const enUS = {
unknownBrowser: "Unknown browser",
lastSeen: "Last active: {{time}}",
signedIn: "Signed in: {{time}}",
+ revokeDevice: "Sign out device",
+ revokeOthers: "Sign out all other devices",
+ revokeDeviceTitle: "Sign out this device?",
+ revokeDeviceDescription: "This session will be revoked immediately and the device will need to sign in again.",
+ revokeOthersTitle: "Sign out all other devices?",
+ revokeOthersDescription: "Every session for this account except the current device will be revoked immediately.",
+ revokeConfirm: "Sign out",
+ revoking: "Signing out...",
+ revokeFailed: "Unable to sign out the device. Please try again.",
},
users: {
title: "Members",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index 938ddb2e..325fc528 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -591,6 +591,15 @@ export const zhCN = {
unknownBrowser: "未知浏览器",
lastSeen: "上次访问:{{time}}",
signedIn: "登录时间:{{time}}",
+ revokeDevice: "退出此设备",
+ revokeOthers: "退出其他所有设备",
+ revokeDeviceTitle: "让此设备退出登录?",
+ revokeDeviceDescription: "该设备的登录会话会立即失效,下次访问时需要重新登录。",
+ revokeOthersTitle: "让其他所有设备退出登录?",
+ revokeOthersDescription: "除当前设备外,当前账号的其他登录会话都会立即失效。",
+ revokeConfirm: "确认退出",
+ revoking: "正在退出...",
+ revokeFailed: "无法退出该设备,请稍后重试。",
},
users: {
title: "成员管理",