This commit is contained in:
hupenglong1
2026-05-22 19:48:47 +08:00
parent 20690cdef3
commit 5311e6d97c
31 changed files with 4620 additions and 472 deletions
+52
View File
@@ -69,6 +69,58 @@ export async function loginAccount(username: string, password: string) {
return createSession(account);
}
const XIAOMI_EMAIL_RE = /^([\w.-]+)@xiaomi\.com$/i;
export function accountIdFromEmail(email: string): string {
const match = XIAOMI_EMAIL_RE.exec((email ?? "").trim());
if (!match) {
throw new Error("请使用小米邮箱(@xiaomi.com)登录");
}
const prefix = match[1].toLowerCase();
if (!/^[a-z0-9._-]{2,40}$/.test(prefix)) {
throw new Error(
"邮箱前缀只能包含小写字母、数字、点、下划线或中划线,长度 2-40",
);
}
return prefix;
}
export async function registerByEmail(email: string, password: string) {
const accountId = accountIdFromEmail(email);
validatePassword(password);
const usersFile = await readUsers();
if (usersFile.users.some((user) => user.id === accountId)) {
throw new Error("该邮箱已注册,请直接登录");
}
const account: UserRecord = {
id: accountId,
username: accountId,
passwordHash: hashPassword(password),
createdAt: new Date().toISOString(),
};
usersFile.users.push(account);
await writeUsers(usersFile);
await createAccountDirectories(account.id);
return createSession(account);
}
export async function loginByEmail(email: string, password: string) {
const accountId = accountIdFromEmail(email);
const usersFile = await readUsers();
const account = usersFile.users.find((user) => user.id === accountId);
if (!account) {
throw new Error("账号不存在,请先注册");
}
if (!account.passwordHash) {
throw new Error("该账号尚未设置密码,请联系管理员重置");
}
if (!verifyPassword(password, account.passwordHash)) {
throw new Error("邮箱或密码错误");
}
await createAccountDirectories(account.id);
return createSession(account);
}
export async function logoutAccount() {
const cookieStore = await cookies();
const token = cookieStore.get(COOKIE_NAME)?.value;