电子邮件一次性密码

电子邮件一次性密码插件允许用户通过发送到其电子邮件地址的一次性密码(OTP)进行登录、验证其电子邮件或重置密码。

安装

将插件添加到你的auth配置中

要在你的应用中启用电子邮件一次性密码,你需要将emailOTP插件添加到你的auth配置中。

auth.ts
import { betterAuth } from "better-auth"
import { emailOTP } from "better-auth/plugins"
 
export const auth = betterAuth({
    // ... 其他配置选项
    plugins: [
        emailOTP({ 
                async sendVerificationOTP({ email, otp, type}) { 
					// 实现sendVerificationOTP方法,将OTP发送到用户的电子邮件地址
				}, 
        }) 
    ]
})

添加客户端插件

auth-client.ts
import { createAuthClient } from "better-auth/client"
import { emailOTPClient } from "better-auth/client/plugins"
 
const authClient =  createAuthClient({
    plugins: [
        emailOTPClient()
    ]
})

使用方法

发送OTP

首先,向用户的电子邮件地址发送一个OTP。

example.ts
const { data, error } = await authClient.emailOtp.sendVerificationOtp({
    email: "[email protected]",
    type: "sign-in" // 或 "email-verification", "forget-password"
})

使用OTP登录

一旦用户提供了OTP,你可以使用signIn.emailOTP()方法让用户登录。

example.ts
const { data, error } = await authClient.signIn.emailOtp({
    email: "[email protected]",
    otp: "123456"
})

如果用户尚未注册,他们将被自动注册。如果你想阻止这种情况,可以在选项中将disableSignUp设置为true

验证电子邮件

要验证用户的电子邮件地址,使用verifyEmail()方法。

example.ts
const { data, error } = await authClient.emailOtp.verifyEmail({
    email: "[email protected]",
    otp: "123456"
})

重置密码

要重置用户的密码,使用resetPassword()方法。

example.ts
const { data, error } = await authClient.emailOtp.resetPassword({
    email: "[email protected]",
    otp: "123456",
    password: "password"
})

选项

  • sendVerificationOTP:一个将OTP发送到用户电子邮件地址的函数。该函数接收一个具有以下属性的对象:
    • email:用户的电子邮件地址。
    • otp:要发送的OTP。
    • type:要发送的OTP类型。可以是"sign-in"、"email-verification"或"forget-password"。

示例

auth.ts
import { betterAuth } from "better-auth"
 
export const auth = betterAuth({
    plugins: [
        emailOTP({
            async sendVerificationOTP({
                email,
                otp,
                type
            }) {
                if (type === "sign-in") {
                    // 发送登录用的OTP
                } else if (type === "email-verification") {
                    // 发送电子邮件验证用的OTP
                } else {
                    // 发送密码重置用的OTP
                }
            },
        })
    ]
})
  • otpLength:OTP的长度。默认为6
  • expiresIn:OTP的过期时间,以秒为单位。默认为300秒。
auth.ts
import { betterAuth } from "better-auth"
 
export const auth = betterAuth({
    plugins: [
        emailOTP({
            otpLength: 8,
            expiresIn: 600
        })
    ]
})
  • sendVerificationOnSignUp:一个布尔值,决定用户注册时是否发送OTP。默认为false

  • disableSignUp:一个布尔值,决定当用户未注册时是否阻止自动注册。默认为false

  • generateOTP:一个生成OTP的函数。默认为随机的6位数字。

  • maxAttempts:验证OTP允许的最大尝试次数。默认为3。超过此限制后,OTP变为无效,用户需要请求新的OTP。

auth.ts
import { betterAuth } from "better-auth"
 
export const auth = betterAuth({
    plugins: [
        emailOTP({
            maxAttempts: 5, // 在使OTP无效前允许5次尝试
            expiresIn: 300
        })
    ]
})

当超过最大尝试次数时,verifyOTPsignIn.emailOtpverifyEmailresetPassword方法将返回代码为MAX_ATTEMPTS_EXCEEDED的错误。

On this page