Files
cnb/agent/routes/user/check.ts
2026-01-12 03:18:09 +08:00

33 lines
981 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { app, cnb } from '@/agent/app.ts';
app.route({
path: 'cnb',
key: 'user-check',
description: '检查用户登录状态参数checkToken,default true; checkCookie, default false',
middleware: ['auth'],
metadata: {
tags: ['opencode']
}
}).define(async (ctx) => {
const checkToken = ctx.query?.checkToken ?? true;
const checkCookie = ctx.query?.checkCookie ?? false;
let output = '';
if (checkToken) {
const res = await cnb.user.getUser();
if (res?.code !== 200) {
output += `Token 无效,请检查 CNB_TOKEN 配置。\n`;
} else {
output += `Token 有效Token用户昵称${res.data?.nickname}\n`;
}
}
if (checkCookie) {
const res = await cnb.user.getCurrentUser();
if (res?.code !== 200) {
output += `Cookie 无效,请检查 CNB_COOKIE 配置。\n`;
} else {
output += `Cookie 有效当前Cookie用户${res.data?.nickname}\n`;
}
}
ctx.body = { output };
}).addTo(app);