feat: 用户注册时间记录(created_at) + 自毁SW + 登录/用户接口微调
Docker Build & Test / build (push) Canceled after 0s
Docker Build & Test / test (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/amd64) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm/v7) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm64) (push) Canceled after 0s
Build & Push Docker image / merge-images (push) Canceled after 0s
Docker Build & Test / build (push) Canceled after 0s
Docker Build & Test / test (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/amd64) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm/v7) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm64) (push) Canceled after 0s
Build & Push Docker image / merge-images (push) Canceled after 0s
This commit is contained in:
1 parent
b861304ad2
commit
980b559296
5 files changed
+88
-3
No files matched your search
@@ -37,6 +37,9 @@ export async function GET(request: NextRequest) {
|
||||
username: user.username,
|
||||
role: user.role || 'user',
|
||||
created_at: user.created_at,
|
||||
created_at_estimated: user.created_at_estimated ?? false,
|
||||
last_login_at: user.last_login_at,
|
||||
last_login_estimated: user.last_login_estimated ?? false,
|
||||
filter_adult_content: settings?.filter_adult_content ?? true,
|
||||
can_disable_filter: settings?.can_disable_filter ?? true,
|
||||
managed_by_admin: settings?.managed_by_admin ?? false,
|
||||
|
||||
@@ -138,6 +138,12 @@ export async function POST(req: NextRequest) {
|
||||
username === process.env.USERNAME &&
|
||||
password === process.env.AUTH_PASSWORD
|
||||
) {
|
||||
// 记录最后登录时间(失败不影响登录)
|
||||
try {
|
||||
await (db as any).touchLogin?.(username);
|
||||
} catch (e) {
|
||||
console.error('记录最后登录时间失败', e);
|
||||
}
|
||||
// 验证成功,设置认证cookie
|
||||
const response = NextResponse.json({ ok: true });
|
||||
const cookieValue = await generateAuthCookie(
|
||||
@@ -178,6 +184,13 @@ export async function POST(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// 记录最后登录时间(失败不影响登录)
|
||||
try {
|
||||
await (db as any).touchLogin?.(username);
|
||||
} catch (e) {
|
||||
console.error('记录最后登录时间失败', e);
|
||||
}
|
||||
|
||||
// 验证成功,设置认证cookie
|
||||
const response = NextResponse.json({ ok: true });
|
||||
const cookieValue = await generateAuthCookie(
|
||||
|
||||
+65
-2
@@ -170,6 +170,43 @@ export class RedisStorage implements IStorage {
|
||||
async registerUser(userName: string, password: string): Promise<void> {
|
||||
// 简单存储明文密码,生产环境应加密
|
||||
await withRetry(() => this.client.set(this.userPwdKey(userName), password));
|
||||
// 记录注册时间(已存在则不覆盖)
|
||||
await this.ensureCreatedAt(userName, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入注册时间。estimated=true 表示该账号早于时间记录功能上线,
|
||||
* 这里记下的是"首次被观测到的时间",前端展示时需标注约等。
|
||||
*/
|
||||
private async ensureCreatedAt(
|
||||
userName: string,
|
||||
estimated: boolean
|
||||
): Promise<void> {
|
||||
const key = `u:${userName}:created_at`;
|
||||
try {
|
||||
const exists = await withRetry(() => this.client.exists(key));
|
||||
if (!exists) {
|
||||
await withRetry(() => this.client.set(key, String(Date.now())));
|
||||
if (estimated) {
|
||||
await withRetry(() => this.client.set(`u:${userName}:created_at_est`, '1'));
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('写入注册时间失败', err);
|
||||
}
|
||||
}
|
||||
|
||||
/** 登录成功后记录最后登录时间 */
|
||||
async touchLogin(userName: string): Promise<void> {
|
||||
try {
|
||||
await withRetry(() =>
|
||||
this.client.set(`u:${userName}:last_login`, String(Date.now()))
|
||||
);
|
||||
// 老账号首次登录时补记注册时间,并标记为估算值
|
||||
await this.ensureCreatedAt(userName, true);
|
||||
} catch (err) {
|
||||
console.error('写入最后登录时间失败', err);
|
||||
}
|
||||
}
|
||||
|
||||
async verifyUser(userName: string, password: string): Promise<boolean> {
|
||||
@@ -313,25 +350,51 @@ export class RedisStorage implements IStorage {
|
||||
})
|
||||
.filter((u): u is string => typeof u === 'string');
|
||||
|
||||
// 获取用户创建时间并构造 User 对象
|
||||
// 获取用户创建时间、最后登录时间并构造 User 对象
|
||||
const users = await Promise.all(
|
||||
usernames.map(async (username) => {
|
||||
// 尝试获取用户创建时间,如果没有则使用空字符串
|
||||
const createdAtKey = `u:${username}:created_at`;
|
||||
let created_at = '';
|
||||
let created_at_estimated = false;
|
||||
try {
|
||||
const timestamp = await withRetry(() => this.client.get(createdAtKey));
|
||||
if (timestamp) {
|
||||
created_at = new Date(parseInt(timestamp)).toISOString();
|
||||
const est = await withRetry(() =>
|
||||
this.client.get(`u:${username}:created_at_est`)
|
||||
);
|
||||
created_at_estimated = ensureString(est ?? '') === '1';
|
||||
}
|
||||
} catch (err) {
|
||||
// 忽略错误,使用空字符串
|
||||
}
|
||||
|
||||
// 最后登录时间(老账号可能从未记录)
|
||||
let last_login_at = '';
|
||||
let last_login_estimated = false;
|
||||
try {
|
||||
const ts = await withRetry(() =>
|
||||
this.client.get(`u:${username}:last_login`)
|
||||
);
|
||||
if (ts) {
|
||||
last_login_at = new Date(parseInt(ensureString(ts))).toISOString();
|
||||
const est = await withRetry(() =>
|
||||
this.client.get(`u:${username}:last_login_est`)
|
||||
);
|
||||
last_login_estimated = ensureString(est ?? '') === '1';
|
||||
}
|
||||
} catch (err) {
|
||||
// 忽略错误
|
||||
}
|
||||
|
||||
return {
|
||||
username,
|
||||
role: username === ownerUsername ? 'owner' : 'user',
|
||||
created_at
|
||||
created_at,
|
||||
created_at_estimated,
|
||||
last_login_at,
|
||||
last_login_estimated
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
@@ -47,6 +47,11 @@ export interface User {
|
||||
username: string;
|
||||
role?: string;
|
||||
created_at?: string;
|
||||
/** 注册时间为补记值(账号早于时间记录功能上线) */
|
||||
created_at_estimated?: boolean;
|
||||
last_login_at?: string;
|
||||
/** 最后登录时间为估算值(按 Redis 键空闲时长推算) */
|
||||
last_login_estimated?: boolean;
|
||||
}
|
||||
|
||||
// 存储接口
|
||||
@@ -70,6 +75,7 @@ export interface IStorage {
|
||||
// 用户相关
|
||||
registerUser(userName: string, password: string): Promise<void>;
|
||||
verifyUser(userName: string, password: string): Promise<boolean>;
|
||||
touchLogin?(userName: string): Promise<void>;
|
||||
// 检查用户是否存在(无需密码)
|
||||
checkUserExist(userName: string): Promise<boolean>;
|
||||
// 修改用户密码
|
||||
|
||||
Reference in new issue
Block a user