Files
learn-languages/src/modules/auth/auth-service.ts
goddonebianu 804c28ada9 refactor: 修复 modules 三层架构违规
- auth: actionDeleteAccount 改用 service+repo,forgot-password 完整三层实现
- card: serviceCheckCardOwnership 替代直接调用 repository
- deck: 移除 service 层的 use server 指令
- dictionary: 数据转换逻辑从 repository 移到 service
- ocr: 认证移到 action 层,跨模块调用改用 service
- translator: genIPA/genLanguage 改用 service 层
2026-03-11 09:40:53 +08:00

102 lines
2.5 KiB
TypeScript

import { auth } from "@/auth";
import {
repoFindUserByUsername,
repoFindUserById,
repoDeleteUserCascade
} from "./auth-repository";
import {
ServiceInputGetUserProfileByUsername,
ServiceInputGetUserProfileById,
ServiceInputSignIn,
ServiceInputSignUp,
ServiceInputDeleteAccount,
ServiceOutputAuth,
ServiceOutputUserProfile,
ServiceOutputDeleteAccount
} from "./auth-service-dto";
/**
* Sign up service
*/
export async function serviceSignUp(dto: ServiceInputSignUp): Promise<ServiceOutputAuth> {
// Better-auth handles user creation internally
const result = await auth.api.signUpEmail({
body: {
email: dto.email,
password: dto.password,
name: dto.name,
username: dto.username,
}
});
if (!result.user) {
return {
success: false,
};
}
return {
success: true,
};
}
/**
* Sign in service
*/
export async function serviceSignIn(dto: ServiceInputSignIn): Promise<ServiceOutputAuth> {
// Try to sign in with username first
const userResult = await repoFindUserByUsername({ username: dto.identifier });
if (userResult) {
// User found by username, use email signIn with the user's email
const result = await auth.api.signInEmail({
body: {
email: userResult.email,
password: dto.password,
}
});
if (result.user) {
return {
success: true,
};
}
} else {
// Try as email
const result = await auth.api.signInEmail({
body: {
email: dto.identifier,
password: dto.password,
}
});
if (result.user) {
return {
success: true,
};
}
}
return {
success: false,
};
}
/**
* Get user profile by username
*/
export async function serviceGetUserProfileByUsername(dto: ServiceInputGetUserProfileByUsername): Promise<ServiceOutputUserProfile> {
return await repoFindUserByUsername(dto);
}
/**
* Get user profile by ID
*/
export async function serviceGetUserProfileById(dto: ServiceInputGetUserProfileById): Promise<ServiceOutputUserProfile> {
return await repoFindUserById(dto);
}
export async function serviceDeleteAccount(dto: ServiceInputDeleteAccount): Promise<ServiceOutputDeleteAccount> {
return await repoDeleteUserCascade({ userId: dto.userId });
}