BetterAuth.ts overview
The BetterAuth instance, typed by the slice this package calls, and the one module that reads auth.$context (BetterAuth’s internal adapter, tables and cookie names).
Since v0.1.0
Exports Grouped by Category
- models
- BetterAuthAdapter (interface)
- BetterAuthApi (interface)
- BetterAuthContext (interface)
- BetterAuthField (interface)
- BetterAuthInternalAdapter (interface)
- BetterAuthLike (interface)
- BetterAuthSession (interface)
- BetterAuthSessionWithUser (interface)
- BetterAuthUser (interface)
- ColumnValue (type alias)
- Columns (type alias)
- SignInAnswer (type alias)
- WithHeaders (interface)
- services
models
BetterAuthAdapter (interface)
BetterAuth’s database adapter, for the lookups the internal adapter has no method for (a session by its stored id, a user by username or phone).
Signature
export interface BetterAuthAdapter {
findOne<T>(query: { model: string; where: Array<{ field: string; value: string }> }): Promise<T | null>
}
Since v0.1.0
BetterAuthApi (interface)
The auth.api endpoints this package calls. signInUsername exists only with BetterAuth’s username() plugin.
Signature
export interface BetterAuthApi {
getSession(context: { headers: Headers; returnHeaders: true }): Promise<WithHeaders<BetterAuthSessionWithUser | null>>
signUpEmail(context: {
body: { email: string; password: string; name: string }
headers: Headers
returnHeaders: true
}): Promise<WithHeaders<SignInAnswer>>
signInEmail(context: {
body: { email: string; password: string; rememberMe?: boolean }
headers: Headers
returnHeaders: true
}): Promise<WithHeaders<SignInAnswer>>
signInUsername?(context: {
body: { username: string; password: string; rememberMe?: boolean }
headers: Headers
returnHeaders: true
}): Promise<WithHeaders<SignInAnswer | null>>
signOut(context: { headers: Headers; returnHeaders: true }): Promise<WithHeaders<{ success: boolean }>>
}
Since v0.1.0
BetterAuthContext (interface)
The resolved auth.$context, by the fields this package reads.
Signature
export interface BetterAuthContext {
readonly internalAdapter: BetterAuthInternalAdapter
readonly adapter: BetterAuthAdapter
readonly tables: Readonly<Record<string, { readonly fields: Readonly<Record<string, BetterAuthField>> } | undefined>>
readonly authCookies: {
readonly sessionToken: {
readonly name: string
readonly attributes: { readonly secure?: boolean | undefined; readonly path?: string }
}
}
}
Since v0.1.0
BetterAuthField (interface)
A column BetterAuth declares on a model, by the one property read here.
Signature
export interface BetterAuthField {
readonly type: string | ReadonlyArray<string>
}
Since v0.1.0
BetterAuthInternalAdapter (interface)
The internal-adapter methods this package calls. They act by user id or token, with no request: BetterAuth’s auth.api has no such call that works without the acting user’s headers.
Signature
export interface BetterAuthInternalAdapter {
findSession(token: string): Promise<BetterAuthSessionWithUser | null>
listSessions(userId: string): Promise<ReadonlyArray<BetterAuthSession>>
deleteSession(token: string): Promise<void>
deleteSessions(tokens: Array<string>): Promise<void>
deleteUserSessions(userId: string): Promise<void>
updateSession(token: string, data: Columns): Promise<BetterAuthSession | null>
findUserById(userId: string): Promise<BetterAuthUser | null>
findUserByEmail(email: string): Promise<{ user: BetterAuthUser } | null>
updateUser(userId: string, data: Columns): Promise<BetterAuthUser | null>
deleteUser(userId: string): Promise<void>
}
Since v0.1.0
BetterAuthLike (interface)
A BetterAuth instance, structurally: what betterAuth({ … }) returns, with or without plugins.
Signature
export interface BetterAuthLike {
readonly api: BetterAuthApi
readonly $context: Promise<BetterAuthContext>
}
Since v0.1.0
BetterAuthSession (interface)
A row of BetterAuth’s session table. token is the bearer credential and never leaves this package; id is the stored id.
Signature
export interface BetterAuthSession {
readonly id: string
readonly token: string
readonly userId: string
readonly expiresAt: Date
readonly createdAt: Date
readonly updatedAt: Date
readonly [column: string]: ColumnValue
}
Since v0.1.0
BetterAuthSessionWithUser (interface)
A session and its user, as getSession and findSession answer them.
Signature
export interface BetterAuthSessionWithUser {
readonly session: BetterAuthSession
readonly user: BetterAuthUser
}
Since v0.1.0
BetterAuthUser (interface)
A row of BetterAuth’s user table, as its server API and internal adapter answer it. Plugins and additionalFields add further columns.
Signature
export interface BetterAuthUser {
readonly id: string
readonly name?: string | null | undefined
readonly email?: string | null | undefined
readonly emailVerified?: boolean | null | undefined
readonly image?: string | null | undefined
readonly createdAt: Date
readonly updatedAt: Date
readonly [column: string]: ColumnValue
}
Since v0.1.0
ColumnValue (type alias)
A column’s value in a BetterAuth row: core, plugin and additionalFields columns alike.
Signature
type ColumnValue =
| string
| number
| boolean
| Date
| null
| undefined
| ReadonlyArray<ColumnValue>
| { readonly [key: string]: ColumnValue }
Since v0.1.0
Columns (type alias)
The columns a write sets.
Signature
type Columns = Readonly<Record<string, ColumnValue>>
Since v0.1.0
SignInAnswer (type alias)
A sign-up or sign-in’s answer. token is null for a sign-up that waits for email verification; BetterAuth’s twoFactor() plugin answers { twoFactorRedirect: true } for a sign-in it holds for the second factor.
Signature
type SignInAnswer =
{ readonly token: string | null; readonly user: BetterAuthUser } | { readonly twoFactorRedirect: true }
Since v0.1.0
WithHeaders (interface)
A server API call’s answer under returnHeaders: true.
Signature
export interface WithHeaders<A> {
readonly headers: Headers
readonly response: A
}
Since v0.1.0
services
BetterAuthInstance (class)
The BetterAuth instance and its resolved context, which every layer in this package is built from.
Signature
declare class BetterAuthInstance
Since v0.1.0