Skip to content

AuthInstance

AuthInstance = object

Defined in: packages/auth/src/types.ts:129

The initialized auth instance with methods for session management, role assignment, impersonation, and request handling.

Created by calling the initAuth() function returned from createAuth with an AuthEnvConfig.

api: unknown

Defined in: packages/auth/src/types.ts:188

The underlying Better Auth instance, for escape-hatch usage.


createContext: (request) => Promise<AuthContext>

Defined in: packages/auth/src/types.ts:134

Builds an AuthContext from the request’s session cookie. Returns a context with user: null if the session is invalid or missing.

Request

Promise<AuthContext>


getRoles: (userId) => Promise<string[]>

Defined in: packages/auth/src/types.ts:144

Retrieves all roles assigned to a user.

string

Promise<string[]>


handler: (request) => Promise<Response>

Defined in: packages/auth/src/types.ts:186

Forwards an HTTP request to the Better Auth handler for processing auth API routes.

Request

Promise<Response>


impersonate: (adminUserId, targetUserId) => Promise<void>

Defined in: packages/auth/src/types.ts:171

Starts an impersonation session where the admin acts as the target user. Only users with roles listed in impersonation.allowedRoles can impersonate.

string

string

Promise<void>

If the admin user’s roles do not permit impersonation.


removeRole: (userId, role) => Promise<void>

Defined in: packages/auth/src/types.ts:164

Removes a single role from a user.

string

string

Promise<void>


requireUser: (request) => Promise<AuthenticatedContext>

Defined in: packages/auth/src/types.ts:142

Like createContext, but redirects to the login page if the user is not authenticated. Sets a cfast_redirect_to cookie so the user returns to the original URL after login.

Request

Promise<AuthenticatedContext>

A 302 redirect response when the user is not authenticated.


sendMagicLink: (params) => Promise<void>

Defined in: packages/auth/src/types.ts:179

Sends a magic link email to the given address for passwordless authentication.

string

Override the post-login redirect URL. Defaults to redirects.afterLogin.

string

The recipient’s email address.

Promise<void>

If the magic link plugin is not configured.


setRole: (userId, role, caller?) => Promise<void>

Defined in: packages/auth/src/types.ts:149

Assigns a single role to a user (additive, does not remove existing roles). When caller.callerRoles is provided, validates against roleGrants rules.

string

string

string[]

Promise<void>


setRoles: (userId, roles, caller?) => Promise<void>

Defined in: packages/auth/src/types.ts:158

Replaces all of a user’s roles with the given set. When caller.callerRoles is provided, validates each role against roleGrants rules.

string

string[]

string[]

Promise<void>


stopImpersonating: (adminUserId) => Promise<void>

Defined in: packages/auth/src/types.ts:173

Ends all active impersonation sessions for the given admin user.

string

Promise<void>