Improve API token format and enhance auth status command

- Change token format to reviq_<base58> prefix instead of raw hex
- Add me.authStatus API endpoint for detailed auth information
- Enhance CLI `reviq auth status` to show token details from API
- Add comprehensive tests for token generation (18 tests)
- Extract bootstrap logic to @reviq/db for reusability and testing
- Remove default db export; callers must use createDb() directly

Token changes:
- New format: reviq_<base58-encoded-32-bytes>
- Added parseToken() for validation
- Added isValidTokenFormat() helper

Auth status endpoint returns:
- User profile information
- Auth method (api_token or session)
- Token/session details (name, expiration, last used)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
RevIQ
2026-01-09 17:59:02 +08:00
parent df9b8808d0
commit 6b9b04d1d0
20 changed files with 764 additions and 125 deletions

View File

@@ -1,6 +1,61 @@
import type { LocalContext } from "../../context.js";
import { buildCommand } from "@stricli/core";
import { createApiClient } from "../../utils/api-client.js";
import { getConfigPath, readConfig } from "../../utils/config.js";
import { TOKEN_PREFIX } from "../../utils/token.js";
interface AuthStatusResponse {
user: {
id: number;
email: string;
displayName: string | null;
fullName: string | null;
isSuperuser: boolean;
emailVerified: boolean;
};
auth:
| {
method: "api_token";
tokenId: string;
tokenName: string;
expiresAt: string;
lastUsedAt: string | null;
createdAt: string;
}
| {
method: "session";
sessionId: string;
expiresAt: string;
createdAt: string;
};
}
function formatDate(dateStr: string): string {
const date = new Date(dateStr);
return date.toLocaleString();
}
function formatRelativeTime(dateStr: string): string {
const date = new Date(dateStr);
const now = new Date();
const diffMs = date.getTime() - now.getTime();
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffDays < 0) {
return `${String(Math.abs(diffDays))} days ago`;
}
if (diffDays === 0) {
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
if (diffHours <= 0) {
return "expired";
}
return `in ${String(diffHours)} hours`;
}
if (diffDays === 1) {
return "tomorrow";
}
return `in ${String(diffDays)} days`;
}
async function status(this: LocalContext): Promise<void> {
const config = await readConfig();
@@ -16,10 +71,66 @@ async function status(this: LocalContext): Promise<void> {
console.log("Authentication Status");
console.log("=====================\n");
console.log(`Email: ${config.email}`);
console.log(`API URL: ${config.apiUrl}`);
console.log(`Config file: ${getConfigPath()}`);
console.log("Token: [configured]");
// Show local config info
console.log("Local Configuration:");
console.log(` Config file: ${getConfigPath()}`);
console.log(` API URL: ${config.apiUrl}`);
// Show token format info
const hasNewFormat = config.token.startsWith(TOKEN_PREFIX);
console.log(
` Token format: ${hasNewFormat ? "reviq_<base58>" : "legacy (hex)"}`,
);
// Try to fetch status from API
console.log("\nAPI Status:");
try {
const client = await createApiClient();
const response = await client.call<AuthStatusResponse>("me.authStatus");
// User info
console.log("\n User:");
console.log(` Email: ${response.user.email}`);
if (response.user.displayName) {
console.log(` Display name: ${response.user.displayName}`);
}
if (response.user.fullName) {
console.log(` Full name: ${response.user.fullName}`);
}
console.log(
` Email verified: ${response.user.emailVerified ? "yes" : "no"}`,
);
console.log(` Superuser: ${response.user.isSuperuser ? "yes" : "no"}`);
// Auth method info
if (response.auth.method === "api_token") {
console.log("\n API Token:");
console.log(` Name: ${response.auth.tokenName}`);
console.log(` Token ID: ${response.auth.tokenId}`);
console.log(` Created: ${formatDate(response.auth.createdAt)}`);
console.log(
` Expires: ${formatDate(response.auth.expiresAt)} (${formatRelativeTime(response.auth.expiresAt)})`,
);
if (response.auth.lastUsedAt) {
console.log(` Last used: ${formatDate(response.auth.lastUsedAt)}`);
}
} else {
console.log("\n Session:");
console.log(` Session ID: ${response.auth.sessionId}`);
console.log(` Created: ${formatDate(response.auth.createdAt)}`);
console.log(
` Expires: ${formatDate(response.auth.expiresAt)} (${formatRelativeTime(response.auth.expiresAt)})`,
);
}
} catch (error) {
console.log(
` Error: ${error instanceof Error ? error.message : String(error)}`,
);
console.log(
"\n Unable to connect to API. Local credentials may be invalid.",
);
}
}
export const statusCommand = buildCommand({