mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-27 01:16:55 -05:00
feat(admin): custom banned user error message (#1692)
* feat(admin): custom banned user error message * docs(admin): add documentation for bannedUserMessage configuration * chore: export types from @better-fetch/fetch in clients * chore: remove update tet helper
This commit is contained in:
@@ -570,3 +570,14 @@ admin({
|
||||
defaultBanExpiresIn: 60 * 60 * 24, // 1 day
|
||||
});
|
||||
```
|
||||
|
||||
### bannedUserMessage
|
||||
|
||||
The message to show when a banned user tries to sign in. Defaults to "You have been banned from this application. Please contact support if you believe this is an error."
|
||||
|
||||
```ts title="auth.ts"
|
||||
admin({
|
||||
bannedUserMessage: "Custom banned user message",
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
@@ -93,3 +93,5 @@ export function createAuthClient<Option extends ClientOptions>(
|
||||
>;
|
||||
};
|
||||
}
|
||||
|
||||
export type * from "@better-fetch/fetch";
|
||||
|
||||
@@ -92,3 +92,5 @@ export function createAuthClient<Option extends ClientOptions>(
|
||||
>;
|
||||
};
|
||||
}
|
||||
|
||||
export type * from "@better-fetch/fetch";
|
||||
|
||||
@@ -140,3 +140,5 @@ export function createAuthClient<Option extends ClientOptions>(
|
||||
>;
|
||||
};
|
||||
}
|
||||
|
||||
export type * from "@better-fetch/fetch";
|
||||
|
||||
@@ -9,7 +9,11 @@ describe("Admin plugin", async () => {
|
||||
const { signInWithTestUser, signInWithUser, cookieSetter, customFetchImpl } =
|
||||
await getTestInstance(
|
||||
{
|
||||
plugins: [admin()],
|
||||
plugins: [
|
||||
admin({
|
||||
bannedUserMessage: "Custom banned user message",
|
||||
}),
|
||||
],
|
||||
databaseHooks: {
|
||||
user: {
|
||||
create: {
|
||||
@@ -260,7 +264,16 @@ describe("Admin plugin", async () => {
|
||||
email: newUser?.email || "",
|
||||
password: "test",
|
||||
});
|
||||
expect(res.error?.status).toBe(401);
|
||||
expect(res.error?.code).toBe("BANNED_USER");
|
||||
expect(res.error?.status).toBe(403);
|
||||
});
|
||||
|
||||
it("should change banned user message", async () => {
|
||||
const res = await client.signIn.email({
|
||||
email: newUser?.email || "",
|
||||
password: "test",
|
||||
});
|
||||
expect(res.error?.message).toBe("Custom banned user message");
|
||||
});
|
||||
|
||||
it("should allow banned user to sign in if ban expired", async () => {
|
||||
|
||||
@@ -88,12 +88,20 @@ export interface AdminOptions {
|
||||
* If this is set, the `adminRole` option is ignored
|
||||
*/
|
||||
adminUserIds?: string[];
|
||||
/**
|
||||
* Message to show when a user is banned
|
||||
*
|
||||
* By default, the message is "You have been banned from this application"
|
||||
*/
|
||||
bannedUserMessage?: string;
|
||||
}
|
||||
|
||||
export const admin = <O extends AdminOptions>(options?: O) => {
|
||||
const opts = {
|
||||
defaultRole: "user",
|
||||
adminRoles: ["admin"],
|
||||
bannedUserMessage:
|
||||
"You have been banned from this application. Please contact support if you believe this is an error.",
|
||||
...options,
|
||||
};
|
||||
type DefaultStatements = typeof defaultStatements;
|
||||
@@ -153,7 +161,11 @@ export const admin = <O extends AdminOptions>(options?: O) => {
|
||||
});
|
||||
return;
|
||||
}
|
||||
return false;
|
||||
|
||||
throw new APIError("FORBIDDEN", {
|
||||
message: opts.bannedUserMessage,
|
||||
code: "BANNED_USER",
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
@@ -16,4 +16,5 @@ export const ADMIN_ERROR_CODES = {
|
||||
YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS: "You are not allowed to delete users",
|
||||
YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD:
|
||||
"You are not allowed to set users password",
|
||||
BANNED_USER: "You have been banned from this application",
|
||||
} as const;
|
||||
|
||||
@@ -218,8 +218,7 @@ export async function getTestInstance<
|
||||
url: string | URL | Request,
|
||||
init?: RequestInit,
|
||||
) => {
|
||||
const req = new Request(url.toString(), init);
|
||||
return auth.handler(req);
|
||||
return auth.handler(new Request(url, init));
|
||||
};
|
||||
|
||||
function sessionSetter(headers: Headers) {
|
||||
|
||||
Reference in New Issue
Block a user