mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-24 16:11:53 -05:00
feat: admin plugin options
This commit is contained in:
@@ -127,8 +127,8 @@ Bans a user, preventing them from signing in and revokes all of their existing s
|
||||
```ts title="admin.ts"
|
||||
const bannedUser = await authClient.admin.banUser({
|
||||
userId: "user_id_here",
|
||||
banReason: "Spamming", // Optional
|
||||
banExpiresIn: 60 * 60 * 24 * 7 // Optional
|
||||
banReason: "Spamming", // Optional (if not provided, the default ban reason will be used - No reason)
|
||||
banExpiresIn: 60 * 60 * 24 * 7 // Optional (if not provided, the ban will never expire)
|
||||
});
|
||||
```
|
||||
|
||||
@@ -174,7 +174,7 @@ const revokedSessions = await authClient.admin.revokeUserSessions({
|
||||
|
||||
### Impersonate User
|
||||
|
||||
This feature allows an admin to create a session that mimics the specified user. The session will remain active until either the browser session ends or it reaches 1 day in age.
|
||||
This feature allows an admin to create a session that mimics the specified user. The session will remain active until either the browser session ends or it reaches 1 hour. You can change this duration by setting the `impersonationSessionDuration` option.
|
||||
|
||||
```ts title="admin.ts"
|
||||
const impersonatedSession = await authClient.admin.impersonateUser({
|
||||
@@ -237,3 +237,48 @@ And adds one field in the `session` table:
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
|
||||
## Options
|
||||
|
||||
### Default Role
|
||||
|
||||
The default role for a user created by the admin. Defaults to `user`.
|
||||
|
||||
```ts title="auth.ts"
|
||||
admin({
|
||||
defaultRole: false //pass false to disable default role assignment
|
||||
})
|
||||
```
|
||||
|
||||
### impersonationSessionDuration
|
||||
|
||||
The duration of the impersonation session in seconds. Defaults to 1 hour.
|
||||
|
||||
```ts title="auth.ts"
|
||||
admin({
|
||||
impersonationSessionDuration: 60 * 60 * 24 // 1 day
|
||||
})
|
||||
```
|
||||
|
||||
### Default Ban Reason
|
||||
|
||||
The default ban reason for a user created by the admin. Defaults to `No reason`.
|
||||
|
||||
```ts title="auth.ts"
|
||||
admin({
|
||||
defaultBanReason: "Spamming"
|
||||
})
|
||||
```
|
||||
|
||||
### Default Ban Expires In
|
||||
|
||||
The default ban expires in for a user created by the admin in seconds. Defaults to `undefined` (meaning the ban never expires).
|
||||
|
||||
```ts title="auth.ts"
|
||||
admin({
|
||||
defaultBanExpiresIn: 60 * 60 * 24 // 1 day
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -110,13 +110,12 @@ export const createInternalAdapter = (
|
||||
userId: string,
|
||||
request?: Request | Headers,
|
||||
dontRememberMe?: boolean,
|
||||
inputData?: Partial<Session> & Record<string, any>,
|
||||
override?: Partial<Session> & Record<string, any>,
|
||||
) => {
|
||||
const headers = request instanceof Request ? request.headers : request;
|
||||
const data: Session = {
|
||||
id: generateId(),
|
||||
userId,
|
||||
...inputData,
|
||||
/**
|
||||
* If the user doesn't want to be remembered
|
||||
* set the session to expire in 1 day.
|
||||
@@ -127,6 +126,7 @@ export const createInternalAdapter = (
|
||||
: getDate(sessionExpiration, "sec"),
|
||||
ipAddress: headers?.get("x-forwarded-for") || "",
|
||||
userAgent: headers?.get("user-agent") || "",
|
||||
...override,
|
||||
};
|
||||
const session = await createWithHooks(data, "session");
|
||||
if (secondaryStorage && session) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
} from "../../api";
|
||||
import type { BetterAuthPlugin, Session, User } from "../../types";
|
||||
import { setSessionCookie } from "../../cookies";
|
||||
import { getDate } from "../../utils/date";
|
||||
|
||||
export interface UserWithRole extends User {
|
||||
role?: string;
|
||||
@@ -38,13 +39,55 @@ export const adminMiddleware = createAuthMiddleware(async (ctx) => {
|
||||
};
|
||||
});
|
||||
|
||||
export const admin = () => {
|
||||
interface AdminOptions {
|
||||
/**
|
||||
* The default role for a user created by the admin
|
||||
*
|
||||
* @default "user"
|
||||
*/
|
||||
defaultRole?: string | false;
|
||||
/**
|
||||
* A default ban reason
|
||||
*
|
||||
* By default, no reason is provided
|
||||
*/
|
||||
defaultBanReason?: string;
|
||||
/**
|
||||
* Number of seconds until the ban expires
|
||||
*
|
||||
* By default, the ban never expires
|
||||
*/
|
||||
defaultBanExpiresIn?: number;
|
||||
/**
|
||||
* Duration of the impersonation session in seconds
|
||||
*
|
||||
* By default, the impersonation session lasts 1 hour
|
||||
*/
|
||||
impersonationSessionDuration?: number;
|
||||
}
|
||||
|
||||
export const admin = (options?: AdminOptions) => {
|
||||
return {
|
||||
id: "admin",
|
||||
init(ctx) {
|
||||
return {
|
||||
options: {
|
||||
databaseHooks: {
|
||||
user: {
|
||||
create: {
|
||||
async before(user) {
|
||||
if (options?.defaultRole === false) {
|
||||
return;
|
||||
}
|
||||
return {
|
||||
data: {
|
||||
...user,
|
||||
role: options?.defaultRole || "user",
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
session: {
|
||||
create: {
|
||||
async before(session) {
|
||||
@@ -265,10 +308,13 @@ export const admin = () => {
|
||||
ctx.body.userId,
|
||||
{
|
||||
banned: true,
|
||||
banReason: ctx.body.banReason,
|
||||
banReason:
|
||||
ctx.body.banReason || options?.defaultBanReason || "No reason",
|
||||
banExpires: ctx.body.banExpiresIn
|
||||
? Date.now() + ctx.body.banExpiresIn * 1000
|
||||
: undefined,
|
||||
: options?.defaultBanExpiresIn
|
||||
? Date.now() + options.defaultBanExpiresIn * 1000
|
||||
: undefined,
|
||||
},
|
||||
);
|
||||
//revoke all sessions
|
||||
@@ -304,6 +350,9 @@ export const admin = () => {
|
||||
true,
|
||||
{
|
||||
impersonatedBy: ctx.context.session.user.id,
|
||||
expiresAt: options?.impersonationSessionDuration
|
||||
? getDate(options.impersonationSessionDuration, "sec")
|
||||
: getDate(60 * 60, "sec"), // 1 hour
|
||||
},
|
||||
);
|
||||
if (!session) {
|
||||
|
||||
Reference in New Issue
Block a user