...
This commit is contained in:
47
src/modules/auth/auth-action-dto.ts
Normal file
47
src/modules/auth/auth-action-dto.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import z from "zod";
|
||||
import { generateValidator } from "@/utils/validate";
|
||||
import { LENGTH_MAX_PASSWORD, LENGTH_MAX_USERNAME, LENGTH_MIN_PASSWORD, LENGTH_MIN_USERNAME } from "@/shared/constant";
|
||||
|
||||
// Schema for sign up
|
||||
const schemaActionInputSignUp = z.object({
|
||||
email: z.string().regex(/^[^\s@]+@[^\s@]+\.[^\s@]+$/, "Invalid email address"),
|
||||
username: z.string().min(LENGTH_MIN_USERNAME).max(LENGTH_MAX_USERNAME).regex(/^[a-zA-Z0-9_]+$/, "Username can only contain letters, numbers, and underscores"),
|
||||
password: z.string().min(LENGTH_MIN_PASSWORD).max(LENGTH_MAX_PASSWORD),
|
||||
redirectTo: z.string().nullish(),
|
||||
});
|
||||
|
||||
export type ActionInputSignUp = z.infer<typeof schemaActionInputSignUp>;
|
||||
|
||||
export const validateActionInputSignUp = generateValidator(schemaActionInputSignUp);
|
||||
|
||||
// Schema for sign in
|
||||
const schemaActionInputSignIn = z.object({
|
||||
identifier: z.string().min(1), // Can be email or username
|
||||
password: z.string().min(LENGTH_MIN_PASSWORD).max(LENGTH_MAX_PASSWORD),
|
||||
redirectTo: z.string().nullish(),
|
||||
});
|
||||
|
||||
export type ActionInputSignIn = z.infer<typeof schemaActionInputSignIn>;
|
||||
|
||||
export const validateActionInputSignIn = generateValidator(schemaActionInputSignIn);
|
||||
|
||||
// Schema for sign out
|
||||
const schemaActionInputSignOut = z.object({
|
||||
redirectTo: z.string().nullish(),
|
||||
});
|
||||
|
||||
export type ActionInputSignOut = z.infer<typeof schemaActionInputSignOut>;
|
||||
|
||||
export const validateActionInputSignOut = generateValidator(schemaActionInputSignOut);
|
||||
|
||||
// Output types
|
||||
export type ActionOutputAuth = {
|
||||
success: boolean;
|
||||
message: string;
|
||||
errors?: {
|
||||
username?: string[];
|
||||
email?: string[];
|
||||
password?: string[];
|
||||
identifier?: string[];
|
||||
};
|
||||
};
|
||||
@@ -3,131 +3,144 @@
|
||||
import { auth } from "@/auth";
|
||||
import { headers } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { ValidateError } from "@/lib/errors";
|
||||
import {
|
||||
ActionInputSignIn,
|
||||
ActionInputSignUp,
|
||||
ActionOutputAuth,
|
||||
validateActionInputSignIn,
|
||||
validateActionInputSignUp
|
||||
} from "./auth-action-dto";
|
||||
import {
|
||||
serviceSignIn,
|
||||
serviceSignUp
|
||||
} from "./auth-service";
|
||||
|
||||
export interface SignUpFormData {
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface SignUpState {
|
||||
success?: boolean;
|
||||
message?: string;
|
||||
errors?: {
|
||||
username?: string[];
|
||||
email?: string[];
|
||||
password?: string[];
|
||||
};
|
||||
}
|
||||
|
||||
export async function signUpAction(prevState: SignUpState, formData: FormData) {
|
||||
const email = formData.get("email") as string;
|
||||
const name = formData.get("name") as string;
|
||||
const password = formData.get("password") as string;
|
||||
const redirectTo = formData.get("redirectTo") as string;
|
||||
|
||||
// 服务器端验证
|
||||
const errors: SignUpState['errors'] = {};
|
||||
|
||||
if (!email) {
|
||||
errors.email = ["邮箱是必填项"];
|
||||
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
errors.email = ["请输入有效的邮箱地址"];
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
errors.username = ["姓名是必填项"];
|
||||
} else if (name.length < 2) {
|
||||
errors.username = ["姓名至少需要2个字符"];
|
||||
}
|
||||
|
||||
if (!password) {
|
||||
errors.password = ["密码是必填项"];
|
||||
} else if (password.length < 8) {
|
||||
errors.password = ["密码至少需要8个字符"];
|
||||
}
|
||||
|
||||
// 如果有验证错误,返回错误状态
|
||||
if (Object.keys(errors).length > 0) {
|
||||
return {
|
||||
success: false,
|
||||
message: "请修正表单中的错误",
|
||||
errors
|
||||
};
|
||||
}
|
||||
// Re-export types for use in components
|
||||
export type { ActionOutputAuth } from "./auth-action-dto";
|
||||
|
||||
/**
|
||||
* Sign up action
|
||||
* Creates a new user account
|
||||
*/
|
||||
export async function actionSignUp(prevState: ActionOutputAuth | undefined, formData: FormData): Promise<ActionOutputAuth> {
|
||||
try {
|
||||
await auth.api.signUpEmail({
|
||||
body: {
|
||||
email,
|
||||
password,
|
||||
name
|
||||
}
|
||||
// Extract form data
|
||||
const rawData = {
|
||||
email: formData.get("email") as string,
|
||||
username: formData.get("username") as string,
|
||||
password: formData.get("password") as string,
|
||||
redirectTo: formData.get("redirectTo") as string | undefined,
|
||||
};
|
||||
|
||||
// Validate input
|
||||
const dto: ActionInputSignUp = validateActionInputSignUp(rawData);
|
||||
|
||||
// Call service layer
|
||||
const result = await serviceSignUp({
|
||||
email: dto.email,
|
||||
username: dto.username,
|
||||
password: dto.password,
|
||||
name: dto.username,
|
||||
});
|
||||
|
||||
redirect(redirectTo || "/");
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('NEXT_REDIRECT')) {
|
||||
throw error;
|
||||
if (!result.success) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Registration failed. Email or username may already be taken.",
|
||||
};
|
||||
}
|
||||
|
||||
// Redirect on success
|
||||
redirect(dto.redirectTo || "/");
|
||||
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.message.includes('NEXT_REDIRECT')) {
|
||||
throw e;
|
||||
}
|
||||
if (e instanceof ValidateError) {
|
||||
return {
|
||||
success: false,
|
||||
message: e.message,
|
||||
};
|
||||
}
|
||||
console.error("Sign up error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "注册失败,请稍后再试"
|
||||
message: "Registration failed. Please try again later.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function signInAction(prevState: SignUpState, formData: FormData) {
|
||||
const email = formData.get("email") as string;
|
||||
const password = formData.get("password") as string;
|
||||
const redirectTo = formData.get("redirectTo") as string;
|
||||
|
||||
// 服务器端验证
|
||||
const errors: SignUpState['errors'] = {};
|
||||
|
||||
if (!email) {
|
||||
errors.email = ["邮箱是必填项"];
|
||||
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
errors.email = ["请输入有效的邮箱地址"];
|
||||
}
|
||||
|
||||
if (!password) {
|
||||
errors.password = ["密码是必填项"];
|
||||
}
|
||||
|
||||
// 如果有验证错误,返回错误状态
|
||||
if (Object.keys(errors).length > 0) {
|
||||
return {
|
||||
success: false,
|
||||
message: "请修正表单中的错误",
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign in action
|
||||
* Authenticates a user
|
||||
*/
|
||||
export async function actionSignIn(_prevState: ActionOutputAuth | undefined, formData: FormData): Promise<ActionOutputAuth> {
|
||||
try {
|
||||
await auth.api.signInEmail({
|
||||
body: {
|
||||
email,
|
||||
password,
|
||||
}
|
||||
// Extract form data
|
||||
const rawData = {
|
||||
identifier: formData.get("identifier") as string,
|
||||
password: formData.get("password") as string,
|
||||
redirectTo: formData.get("redirectTo") as string | undefined,
|
||||
};
|
||||
|
||||
// Validate input
|
||||
const dto: ActionInputSignIn = validateActionInputSignIn(rawData);
|
||||
|
||||
// Call service layer
|
||||
const result = await serviceSignIn({
|
||||
identifier: dto.identifier,
|
||||
password: dto.password,
|
||||
});
|
||||
|
||||
redirect(redirectTo || "/");
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('NEXT_REDIRECT')) {
|
||||
throw error;
|
||||
if (!result.success) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Invalid email/username or password.",
|
||||
errors: {
|
||||
identifier: ["Invalid email/username or password"],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Redirect on success
|
||||
redirect(dto.redirectTo || "/");
|
||||
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.message.includes('NEXT_REDIRECT')) {
|
||||
throw e;
|
||||
}
|
||||
if (e instanceof ValidateError) {
|
||||
return {
|
||||
success: false,
|
||||
message: e.message,
|
||||
};
|
||||
}
|
||||
console.error("Sign in error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "登录失败,请检查您的邮箱和密码"
|
||||
message: "Sign in failed. Please check your credentials.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign out action
|
||||
* Signs out the current user
|
||||
*/
|
||||
export async function signOutAction() {
|
||||
await auth.api.signOut({
|
||||
headers: await headers()
|
||||
});
|
||||
try {
|
||||
await auth.api.signOut({
|
||||
headers: await headers()
|
||||
});
|
||||
|
||||
redirect("/auth");
|
||||
redirect("/auth");
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.message.includes('NEXT_REDIRECT')) {
|
||||
throw e;
|
||||
}
|
||||
console.error("Sign out error:", e);
|
||||
redirect("/auth");
|
||||
}
|
||||
}
|
||||
|
||||
50
src/modules/auth/auth-service-dto.ts
Normal file
50
src/modules/auth/auth-service-dto.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// Service layer DTOs for auth module
|
||||
|
||||
// Sign up input/output
|
||||
export type ServiceInputSignUp = {
|
||||
email: string;
|
||||
username: string;
|
||||
password: string; // plain text, will be hashed by better-auth
|
||||
name: string;
|
||||
};
|
||||
|
||||
export type ServiceOutputSignUp = {
|
||||
success: boolean;
|
||||
userId?: string;
|
||||
email?: string;
|
||||
username?: string;
|
||||
};
|
||||
|
||||
// Sign in input/output
|
||||
export type ServiceInputSignIn = {
|
||||
identifier: string; // email or username
|
||||
password: string;
|
||||
};
|
||||
|
||||
export type ServiceOutputSignIn = {
|
||||
success: boolean;
|
||||
userId?: string;
|
||||
email?: string;
|
||||
username?: string;
|
||||
sessionToken?: string;
|
||||
};
|
||||
|
||||
// Sign out input/output
|
||||
export type ServiceInputSignOut = {
|
||||
sessionId?: string;
|
||||
};
|
||||
|
||||
export type ServiceOutputSignOut = {
|
||||
success: boolean;
|
||||
};
|
||||
|
||||
// User existence check
|
||||
export type ServiceInputCheckUserExists = {
|
||||
email?: string;
|
||||
username?: string;
|
||||
};
|
||||
|
||||
export type ServiceOutputCheckUserExists = {
|
||||
emailExists: boolean;
|
||||
usernameExists: boolean;
|
||||
};
|
||||
76
src/modules/auth/auth-service.ts
Normal file
76
src/modules/auth/auth-service.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { auth } from "@/auth";
|
||||
import {
|
||||
ServiceInputSignUp,
|
||||
ServiceInputSignIn,
|
||||
ServiceOutputSignUp,
|
||||
ServiceOutputSignIn
|
||||
} from "./auth-service-dto";
|
||||
|
||||
/**
|
||||
* Sign up a new user
|
||||
* Calls better-auth's signUp.email with username support
|
||||
*/
|
||||
export async function serviceSignUp(dto: ServiceInputSignUp): Promise<ServiceOutputSignUp> {
|
||||
try {
|
||||
await auth.api.signUpEmail({
|
||||
body: {
|
||||
email: dto.email,
|
||||
password: dto.password,
|
||||
username: dto.username,
|
||||
name: dto.name,
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
email: dto.email,
|
||||
username: dto.username,
|
||||
};
|
||||
} catch (error) {
|
||||
// better-auth handles duplicates and validation errors
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign in user
|
||||
* Uses better-auth's signIn.username for username-based authentication
|
||||
*/
|
||||
export async function serviceSignIn(dto: ServiceInputSignIn): Promise<ServiceOutputSignIn> {
|
||||
try {
|
||||
// Determine if identifier is email or username
|
||||
const isEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(dto.identifier);
|
||||
|
||||
let session;
|
||||
|
||||
if (isEmail) {
|
||||
// Use email sign in
|
||||
session = await auth.api.signInEmail({
|
||||
body: {
|
||||
email: dto.identifier,
|
||||
password: dto.password,
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Use username sign in (requires username plugin)
|
||||
session = await auth.api.signInUsername({
|
||||
body: {
|
||||
username: dto.identifier,
|
||||
password: dto.password,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
sessionToken: session?.token,
|
||||
};
|
||||
} catch (error) {
|
||||
// better-auth throws on invalid credentials
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user