fix: return proper error on conflict username

This commit is contained in:
Bereket Engida
2024-10-21 18:33:55 +03:00
parent 3c12067e6c
commit 72243d2f78
3 changed files with 22 additions and 11 deletions

View File

@@ -69,7 +69,6 @@ export const signUpEmail = <O extends BetterAuthOptions>() =>
message: "Password is too long",
});
}
const dbUser = await ctx.context.internalAdapter.findUserByEmail(email);
if (dbUser?.user) {
ctx.context.logger.info(`Sign-up attempt for existing email: ${email}`);
@@ -82,15 +81,28 @@ export const signUpEmail = <O extends BetterAuthOptions>() =>
ctx.context.options,
additionalFields as any,
);
const createdUser = await ctx.context.internalAdapter.createUser({
email: email.toLowerCase(),
name,
image,
...additionalData,
emailVerified: false,
});
let createdUser: User;
try {
createdUser = await ctx.context.internalAdapter.createUser({
email: email.toLowerCase(),
name,
image,
...additionalData,
emailVerified: false,
});
if (!createdUser) {
throw new APIError("BAD_REQUEST", {
message: "Failed to create user",
});
}
} catch (e) {
throw new APIError("UNPROCESSABLE_ENTITY", {
message: "Failed to create user",
details: e,
});
}
if (!createdUser) {
throw new APIError("BAD_REQUEST", {
throw new APIError("UNPROCESSABLE_ENTITY", {
message: "Failed to create user",
});
}

View File

@@ -42,7 +42,6 @@ export const updateUser = <O extends BetterAuthOptions>() =>
});
}
const additionalFields = parseUserInput(ctx.context.options, rest);
console.log({ additionalFields });
const user = await ctx.context.internalAdapter.updateUserByEmail(
session.user.email,
{

View File

@@ -25,7 +25,7 @@ describe("username", async (it) => {
expect(res.data?.user.username).toBe("new-username");
});
const headers = new Headers();
it("should signin with username", async () => {
it("should sign-in with username", async () => {
const res = await client.signIn.username(
{
username: "new-username",