better-auth-authentication

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Better Auth Authentication

Better Auth 认证

Goals

目标

  • Enable email/password authentication and social providers.
  • Implement sign-up, sign-in, sign-out, and verification flows.
  • Handle redirects and errors consistently.
  • 启用邮箱/密码认证和社交登录提供商。
  • 实现注册、登录、登出和验证流程。
  • 统一处理重定向和错误。

Quick start

快速开始

  1. Enable
    emailAndPassword
    and configure
    socialProviders
    .
  2. Create a client with
    createAuthClient
    .
  3. Use
    signUp.email
    ,
    signIn.email
    ,
    signIn.social
    , and
    signOut
    on the client.
ts
import { betterAuth } from "better-auth";

export const auth = betterAuth({
  emailAndPassword: { enabled: true },
  socialProviders: {
    github: {
      clientId: process.env.GITHUB_CLIENT_ID as string,
      clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
    },
  },
});
ts
import { createAuthClient } from "better-auth/client";

const authClient = createAuthClient();

await authClient.signUp.email({
  email,
  password,
  name,
});

await authClient.signIn.email({
  email,
  password,
  callbackURL: "/dashboard",
});

await authClient.signIn.social({
  provider: "github",
  callbackURL: "/dashboard",
});

await authClient.signOut();
  1. 启用
    emailAndPassword
    并配置
    socialProviders
  2. 使用
    createAuthClient
    创建客户端。
  3. 在客户端上调用
    signUp.email
    signIn.email
    signIn.social
    signOut
    方法。
ts
import { betterAuth } from "better-auth";

export const auth = betterAuth({
  emailAndPassword: { enabled: true },
  socialProviders: {
    github: {
      clientId: process.env.GITHUB_CLIENT_ID as string,
      clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
    },
  },
});
ts
import { createAuthClient } from "better-auth/client";

const authClient = createAuthClient();

await authClient.signUp.email({
  email,
  password,
  name,
});

await authClient.signIn.email({
  email,
  password,
  callbackURL: "/dashboard",
});

await authClient.signIn.social({
  provider: "github",
  callbackURL: "/dashboard",
});

await authClient.signOut();

Email verification

邮箱验证

  • Provide
    emailVerification.sendVerificationEmail
    to send the verification link.
  • Use
    emailAndPassword.requireEmailVerification
    to enforce verification before sign-in.
  • 调用
    emailVerification.sendVerificationEmail
    发送验证链接。
  • 使用
    emailAndPassword.requireEmailVerification
    强制用户在登录前完成验证。

Social providers

社交登录提供商

  • Configure providers in
    socialProviders
    with provider-specific credentials.
  • Use
    signIn.social
    to start OAuth flows.
  • Pass
    callbackURL
    ,
    errorCallbackURL
    , and
    newUserCallbackURL
    for redirects.
  • socialProviders
    中配置各提供商的专属凭证。
  • 使用
    signIn.social
    启动OAuth流程。
  • 传入
    callbackURL
    errorCallbackURL
    newUserCallbackURL
    以处理重定向。

Guardrails

注意事项

  • Call client methods from the client only.
  • Keep secrets in server-only env variables.
  • Use
    rememberMe
    to control persistent sessions on email/password sign-in.
  • 仅在客户端调用客户端方法。
  • 密钥需存储在仅服务器可访问的环境变量中。
  • 在邮箱/密码登录时使用
    rememberMe
    控制会话的持久性。

References

参考文档

  • toolchains/platforms/auth/better-auth/better-auth-authentication/references/email-password.md
  • toolchains/platforms/auth/better-auth/better-auth-authentication/references/providers.md
  • toolchains/platforms/auth/better-auth/better-auth-authentication/references/email-password.md
  • toolchains/platforms/auth/better-auth/better-auth-authentication/references/providers.md