mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-31 19:36:46 -05:00
fix(username): sign in should work with post normalization (#4599)
Co-authored-by: Alex Yang <himself65@outlook.com>
This commit is contained in:
@@ -264,13 +264,13 @@ export const username = (options?: UsernameOptions) => {
|
||||
}
|
||||
|
||||
const user = await ctx.context.adapter.findOne<
|
||||
User & { username: string }
|
||||
User & { username: string; displayUsername: string }
|
||||
>({
|
||||
model: "user",
|
||||
where: [
|
||||
{
|
||||
field: "username",
|
||||
value: username,
|
||||
value: normalizer(username),
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -358,6 +358,7 @@ export const username = (options?: UsernameOptions) => {
|
||||
email: user.email,
|
||||
emailVerified: user.emailVerified,
|
||||
username: user.username,
|
||||
displayUsername: user.displayUsername,
|
||||
name: user.name,
|
||||
image: user.image,
|
||||
createdAt: user.createdAt,
|
||||
|
||||
@@ -269,6 +269,31 @@ describe("username", async (it) => {
|
||||
expect(session?.user.username).toBe("custom_user");
|
||||
expect(session?.user.displayUsername).toBe("Fancy Display Name");
|
||||
});
|
||||
|
||||
it("should sign in with normalized username", async () => {
|
||||
const { client } = await getTestInstance(
|
||||
{
|
||||
plugins: [username()],
|
||||
},
|
||||
{
|
||||
clientOptions: {
|
||||
plugins: [usernameClient()],
|
||||
},
|
||||
},
|
||||
);
|
||||
await client.signUp.email({
|
||||
email: "normalized-username@email.com",
|
||||
username: "Custom_User",
|
||||
password: "test-password",
|
||||
name: "test-name",
|
||||
});
|
||||
const res2 = await client.signIn.username({
|
||||
username: "Custom_User",
|
||||
password: "test-password",
|
||||
});
|
||||
expect(res2.data?.user.username).toBe("custom_user");
|
||||
expect(res2.data?.user.displayUsername).toBe("Custom_User");
|
||||
});
|
||||
});
|
||||
|
||||
describe("username custom normalization", async (it) => {
|
||||
|
||||
Reference in New Issue
Block a user