mirror of
https://github.com/better-auth/better-auth.git
synced 2026-07-30 06:04:15 -05:00
fix(username): username should respect send on sign config (#4799)
This commit is contained in:
@@ -4,11 +4,11 @@ import type { BetterAuthPlugin } from "../../types/plugins";
|
||||
import { APIError } from "better-call";
|
||||
import type { Account, InferOptionSchema, User } from "../../types";
|
||||
import { setSessionCookie } from "../../cookies";
|
||||
import { sendVerificationEmailFn } from "../../api";
|
||||
import { BASE_ERROR_CODES } from "../../error/codes";
|
||||
import { getSchema, type UsernameSchema } from "./schema";
|
||||
import { mergeSchema } from "../../db/schema";
|
||||
import { USERNAME_ERROR_CODES as ERROR_CODES } from "./error-codes";
|
||||
import { createEmailVerificationToken } from "../../api";
|
||||
export * from "./error-codes";
|
||||
export type UsernameOptions = {
|
||||
schema?: InferOptionSchema<UsernameSchema>;
|
||||
@@ -287,10 +287,37 @@ export const username = (options?: UsernameOptions) => {
|
||||
}
|
||||
|
||||
if (
|
||||
!user.emailVerified &&
|
||||
ctx.context.options.emailAndPassword?.requireEmailVerification
|
||||
ctx.context.options?.emailAndPassword?.requireEmailVerification &&
|
||||
!user.emailVerified
|
||||
) {
|
||||
await sendVerificationEmailFn(ctx, user);
|
||||
if (
|
||||
!ctx.context.options?.emailVerification?.sendVerificationEmail
|
||||
) {
|
||||
throw new APIError("FORBIDDEN", {
|
||||
message: ERROR_CODES.EMAIL_NOT_VERIFIED,
|
||||
});
|
||||
}
|
||||
|
||||
if (ctx.context.options?.emailVerification?.sendOnSignIn) {
|
||||
const token = await createEmailVerificationToken(
|
||||
ctx.context.secret,
|
||||
user.email,
|
||||
undefined,
|
||||
ctx.context.options.emailVerification?.expiresIn,
|
||||
);
|
||||
const url = `${ctx.context.baseURL}/verify-email?token=${token}&callbackURL=${
|
||||
ctx.body.callbackURL || "/"
|
||||
}`;
|
||||
await ctx.context.options.emailVerification.sendVerificationEmail(
|
||||
{
|
||||
user: user,
|
||||
url,
|
||||
token,
|
||||
},
|
||||
ctx.request,
|
||||
);
|
||||
}
|
||||
|
||||
throw new APIError("FORBIDDEN", {
|
||||
message: ERROR_CODES.EMAIL_NOT_VERIFIED,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user