Skip to content

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

feat: show active login devices

tianma-if576f6219488b
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 3f24aef5..f89a8c49 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -490,6 +490,44 @@ app.get("/api/v1/auth/session", async (c) => {
});
});
+app.get("/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();
+ const rows = await c.env.DB.prepare(
+ `SELECT id, user_agent, expires_at, created_at, last_seen_at
+ FROM sessions
+ WHERE user_id = ?
+ AND revoked_at IS NULL
+ AND expires_at > ?
+ ORDER BY COALESCE(last_seen_at, created_at) DESC
+ LIMIT 50`
+ )
+ .bind(auth.actorId, now)
+ .all<{
+ id: string;
+ user_agent: string | null;
+ expires_at: string;
+ created_at: string;
+ last_seen_at: string | null;
+ }>();
+
+ return c.json({
+ sessions: rows.results.map((session) => ({
+ id: session.id,
+ userAgent: session.user_agent,
+ isCurrent: session.id === auth.sessionId,
+ createdAt: session.created_at,
+ lastSeenAt: session.last_seen_at ?? session.created_at,
+ expiresAt: session.expires_at,
+ })),
+ });
+});
+
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 2f33cf41..b81bc85b 100644
--- a/apps/web/src/components/SettingsPane.tsx
+++ b/apps/web/src/components/SettingsPane.tsx
@@ -16,6 +16,7 @@ import { WORKSPACE_PAGE_TITLE_CLASSNAME } from "@/lib/workspace-ui";
import { cn } from "@/lib/utils";
import { AdvancedPlayCard } from "./settings/AdvancedPlayCard";
import { DataExportCard } from "./settings/DataExportCard";
+import { LoginDevicesCard } from "./settings/LoginDevicesCard";
import { EvernoteImportGuideCard } from "./settings/EvernoteImportGuideCard";
import { FeedbackLink } from "./settings/FeedbackLink";
import { McpConfigCard } from "./settings/McpConfigCard";
@@ -200,6 +201,7 @@ export const SettingsPane = ({
return (
<SettingsGroup>
<PasswordCard authRequired={authRequired} demoMode={demoMode} />
+ <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
new file mode 100644
index 00000000..c0e25140
--- /dev/null
+++ b/apps/web/src/components/settings/LoginDevicesCard.tsx
@@ -0,0 +1,125 @@
+import { useQuery } from "@tanstack/react-query";
+import { Laptop, MonitorSmartphone, Smartphone, Tablet } from "lucide-react";
+import { useTranslation } from "react-i18next";
+import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
+import { api } from "@/lib/api";
+
+interface LoginDevicesCardProps {
+ authRequired: boolean;
+}
+
+type DeviceKind = "mobile" | "tablet" | "desktop" | "unknownDevice";
+
+const describeUserAgent = (userAgent: string | null) => {
+ if (!userAgent) return { deviceKind: "unknownDevice" as const, os: "", browser: "" };
+
+ const isTablet = /iPad|Tablet|PlayBook|Silk/i.test(userAgent) || (/Android/i.test(userAgent) && !/Mobile/i.test(userAgent));
+ const isMobile = !isTablet && /Mobile|iPhone|iPod|Android|IEMobile|Opera Mini|okhttp/i.test(userAgent);
+ const deviceKind: DeviceKind = isTablet ? "tablet" : isMobile ? "mobile" : "desktop";
+ const os = /Windows NT/i.test(userAgent)
+ ? "Windows"
+ : /iPad|iPhone|iPod/i.test(userAgent)
+ ? "iOS"
+ : /Android/i.test(userAgent)
+ ? "Android"
+ : /Mac OS X|Macintosh/i.test(userAgent)
+ ? "macOS"
+ : /CrOS/i.test(userAgent)
+ ? "ChromeOS"
+ : /Linux/i.test(userAgent)
+ ? "Linux"
+ : "";
+ const browser = /EdgeEver|okhttp/i.test(userAgent)
+ ? "EdgeEver"
+ : /EdgA?\//i.test(userAgent)
+ ? "Edge"
+ : /OPR\//i.test(userAgent)
+ ? "Opera"
+ : /CriOS|Chrome\//i.test(userAgent)
+ ? "Chrome"
+ : /FxiOS|Firefox\//i.test(userAgent)
+ ? "Firefox"
+ : /Safari\//i.test(userAgent)
+ ? "Safari"
+ : "";
+
+ return { deviceKind, os, browser };
+};
+
+const getDeviceIcon = (deviceKind: DeviceKind) => {
+ if (deviceKind === "mobile") return Smartphone;
+ if (deviceKind === "tablet") return Tablet;
+ if (deviceKind === "desktop") return Laptop;
+ return MonitorSmartphone;
+};
+
+const formatSessionTime = (value: string, locale: string) =>
+ new Intl.DateTimeFormat(locale, {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ hour: "2-digit",
+ minute: "2-digit",
+ }).format(new Date(value));
+
+export const LoginDevicesCard = ({ authRequired }: LoginDevicesCardProps) => {
+ const { t, i18n } = useTranslation();
+ const locale = i18n.resolvedLanguage ?? i18n.language;
+ const sessionsQuery = useQuery({
+ queryKey: ["auth", "sessions"],
+ queryFn: api.listLoginDeviceSessions,
+ enabled: authRequired,
+ });
+
+ if (!authRequired) return null;
+
+ 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(" · ");
+
+ 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>
+ </li>
+ );
+ })}
+ </ul>
+ ) : null}
+ </CardContent>
+ </Card>
+ );
+};
diff --git a/apps/web/src/components/settings/PasswordCard.tsx b/apps/web/src/components/settings/PasswordCard.tsx
index 517d985a..86273b7e 100644
--- a/apps/web/src/components/settings/PasswordCard.tsx
+++ b/apps/web/src/components/settings/PasswordCard.tsx
@@ -71,10 +71,11 @@ export const PasswordCard = ({ authRequired, demoMode }: PasswordCardProps) => {
{t("password.demoReadOnly")}
</p>
) : (
- <form className="grid gap-3" onSubmit={handleSubmit}>
+ <form className="grid gap-3 lg:grid-cols-3 lg:gap-y-2.5" onSubmit={handleSubmit}>
<label className="grid gap-1.5 text-sm font-medium text-slate-700">
{t("password.currentPassword")}
<Input
+ className="lg:h-9"
type="password"
autoComplete="current-password"
value={currentPassword}
@@ -85,6 +86,7 @@ export const PasswordCard = ({ authRequired, demoMode }: PasswordCardProps) => {
<label className="grid gap-1.5 text-sm font-medium text-slate-700">
{t("password.newPassword")}
<Input
+ className="lg:h-9"
type="password"
autoComplete="new-password"
minLength={8}
@@ -96,6 +98,7 @@ export const PasswordCard = ({ authRequired, demoMode }: PasswordCardProps) => {
<label className="grid gap-1.5 text-sm font-medium text-slate-700">
{t("password.confirmPassword")}
<Input
+ className="lg:h-9"
type="password"
autoComplete="new-password"
minLength={8}
@@ -104,14 +107,14 @@ export const PasswordCard = ({ authRequired, demoMode }: PasswordCardProps) => {
required
/>
</label>
- <div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
+ <div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between lg:col-span-3">
<p
className={feedback?.type === "error" ? "text-xs font-medium text-rose-600" : "text-xs font-medium text-emerald-700"}
role={feedback ? "status" : undefined}
>
{feedback?.message}
</p>
- <Button className="w-full bg-emerald-600 text-white hover:bg-emerald-700 sm:w-auto" type="submit" disabled={isSubmitting}>
+ <Button className="w-full bg-emerald-600 text-white hover:bg-emerald-700 sm:w-auto lg:h-9 lg:px-3" type="submit" disabled={isSubmitting}>
{isSubmitting ? t("password.changing") : t("password.change")}
</Button>
</div>
diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts
index d8f8210c..282e8c7a 100644
--- a/apps/web/src/lib/api.ts
+++ b/apps/web/src/lib/api.ts
@@ -1,5 +1,6 @@
import type {
AuthSession,
+ LoginDeviceSession,
InstanceUser,
ApiToken,
CreatedApiToken,
@@ -49,6 +50,7 @@ type ListApiTokensResponse = {
type ListUsersResponse = { users: InstanceUser[] };
type UserResponse = { user: InstanceUser };
+type ListLoginDeviceSessionsResponse = { sessions: LoginDeviceSession[] };
type MemoResponse = {
memo: MemoDetail;
@@ -119,6 +121,9 @@ const request = async <T>(path: string, init?: RequestInit): Promise<T> => {
export const api = {
getSession: () => request<AuthSession>("/api/v1/auth/session"),
+ listLoginDeviceSessions: () =>
+ request<ListLoginDeviceSessionsResponse>("/api/v1/auth/sessions"),
+
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 ab32a0ed..218824d1 100644
--- a/docs/openapi.json
+++ b/docs/openapi.json
@@ -88,6 +88,33 @@
}
}
},
+ "/api/v1/auth/sessions": {
+ "get": {
+ "summary": "List the current user's active login sessions",
+ "responses": {
+ "200": {
+ "description": "Active login sessions ordered by latest activity",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "sessions": {
+ "type": "array",
+ "items": { "$ref": "#/components/schemas/LoginDeviceSession" }
+ }
+ },
+ "required": ["sessions"]
+ }
+ }
+ }
+ },
+ "401": {
+ "$ref": "#/components/responses/Error"
+ }
+ }
+ }
+ },
"/api/v1/auth/login": {
"post": {
"summary": "Create interactive session",
@@ -1316,6 +1343,18 @@
},
"required": ["authRequired", "authenticated", "demoMode", "user"]
},
+ "LoginDeviceSession": {
+ "type": "object",
+ "properties": {
+ "id": { "type": "string" },
+ "userAgent": { "type": ["string", "null"] },
+ "isCurrent": { "type": "boolean" },
+ "createdAt": { "type": "string", "format": "date-time" },
+ "lastSeenAt": { "type": "string", "format": "date-time" },
+ "expiresAt": { "type": "string", "format": "date-time" }
+ },
+ "required": ["id", "userAgent", "isCurrent", "createdAt", "lastSeenAt", "expiresAt"]
+ },
"AuthUser": {
"type": "object",
"properties": {
diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts
index 2ab886f6..be234e3a 100644
--- a/packages/client/src/index.ts
+++ b/packages/client/src/index.ts
@@ -1,6 +1,7 @@
import type {
ApiToken,
AuthSession,
+ LoginDeviceSession,
InstanceUser,
CreatedApiToken,
JsonBackupMemo,
@@ -64,6 +65,10 @@ export type UserResponse = {
user: InstanceUser;
};
+export type ListLoginDeviceSessionsResponse = {
+ sessions: LoginDeviceSession[];
+};
+
export type MemoResponse = {
memo: MemoDetail;
};
@@ -167,6 +172,9 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {
return {
getSession: () => request<AuthSession>("/api/v1/auth/session"),
+ listLoginDeviceSessions: () =>
+ request<ListLoginDeviceSessionsResponse>("/api/v1/auth/sessions"),
+
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 2d7d4f89..ecef92f1 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -577,6 +577,21 @@ export const enUS = {
loggingOut: "Signing out...",
logout: "Sign out",
},
+ loginDevices: {
+ title: "Signed-in devices",
+ description: "View devices where this account is still signed in and their latest activity.",
+ loading: "Loading signed-in devices...",
+ loadFailed: "Unable to load signed-in devices. Please try again.",
+ empty: "No active signed-in devices.",
+ current: "Current device",
+ mobile: "Phone",
+ tablet: "Tablet",
+ desktop: "Computer",
+ unknownDevice: "Unknown device",
+ unknownBrowser: "Unknown browser",
+ lastSeen: "Last active: {{time}}",
+ signedIn: "Signed in: {{time}}",
+ },
users: {
title: "Members",
description: "Create isolated personal note spaces for family or team members. Public registration stays disabled.",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index ddbb79b4..938ddb2e 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -577,6 +577,21 @@ export const zhCN = {
loggingOut: "安全退出中...",
logout: "退出登录",
},
+ loginDevices: {
+ title: "登录设备",
+ description: "查看当前账号仍保持登录的设备及最近访问时间。",
+ loading: "正在加载登录设备...",
+ loadFailed: "登录设备加载失败,请稍后重试。",
+ empty: "暂无有效的登录设备。",
+ current: "当前设备",
+ mobile: "手机",
+ tablet: "平板",
+ desktop: "电脑",
+ unknownDevice: "未知设备",
+ unknownBrowser: "未知浏览器",
+ lastSeen: "上次访问:{{time}}",
+ signedIn: "登录时间:{{time}}",
+ },
users: {
title: "成员管理",
description: "为家人或团队成员创建独立的个人笔记空间。实例不开放公开注册。",
diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts
index 44748899..a894fd1a 100644
--- a/packages/shared/src/types.ts
+++ b/packages/shared/src/types.ts
@@ -134,6 +134,15 @@ export type AuthSession = {
sessionToken?: string;
};
+export type LoginDeviceSession = {
+ id: string;
+ userAgent: string | null;
+ isCurrent: boolean;
+ createdAt: string;
+ lastSeenAt: string;
+ expiresAt: string;
+};
+
export type ApiError = {
error: {
code: string;