Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Infer `AuthUser` / `AuthSession` from `auth.ts`: plugin and `additionalFields` columns are missing from `c.get('user')`, `getUser()` and `requireAuth()`

未关闭
#567 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
53/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
typescript

调研方向

Start with packages/void/src/runtime/auth.ts and the CloudContextVariables definitions in packages/void/src/runtime/handler.ts; trace how AuthUser and AuthSession flow into getUser(), getSession() and requireAuth(c). Check how .void/env.d.ts generates inferred types and use the Better Auth config type and the issue’s reproduction to verify the expected fields appear. Done when those APIs type the configured plugin and additional fields correctly.

由索引模型根据 Issue 内容生成。

描述

Description

AuthUser and AuthSession are always Better Auth's base shapes, whatever auth.ts configures. Fields added by plugins (role and banned from admin(), impersonatedBy on the session) or by user.additionalFields are present at runtime but missing from the types of c.get('user'), c.get('session'), getUser(), getSession() and requireAuth(c).

The app can't fix this itself:

  • Redeclaring user on CloudContextVariables fails with TS2717, because the property already has a different type.
  • AuthUser is a type alias, so it can't be augmented.

A cast at every read is the only option.

Void already has the information. defineAuth<T>(config: T): T keeps the config type, and .void/better-auth-schema.ts is generated with the plugin columns.

Steps to reproduce

  1. Enable auth and add auth.ts with a plugin and a custom field:

    // auth.ts
    import { admin } from 'better-auth/plugins/admin';
    import { defineAuth } from 'void/auth';
    
    export default defineAuth(({ defaults }) => ({
      ...defaults,
      plugins: [admin()],
      user: { additionalFields: { plan: { type: 'string', required: false } } },
    }));
    
  2. Read the fields on the server:

    import { defineHandler } from 'void';
    import { getUser } from 'void/auth';
    
    export const loader = defineHandler((c) => ({
      role: c.get('user')?.role,
      plan: getUser()?.plan,
    }));
    
  3. Run tsc.

Actual behavior

error TS2339: Property 'role' does not exist on type '{ id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }'.
error TS2339: Property 'plan' does not exist on type '{ id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }'.

Trying to override the type:

declare module 'void' {
  interface CloudContextVariables {
    user: (AuthUser & { role: string | null }) | null;
  }
}
error TS2717: Subsequent property declarations must have the same type. Property 'user' must be of type '{ id: string; … } | null', but here has type '… | null'.

Expected behavior

c.get('user'), getUser() and requireAuth(c) include role, banned, banReason, banExpires and plan. c.get('session') and getSession() include impersonatedBy.

Cause

packages/void/src/runtime/auth.ts (main @ a6de7829bf, lines 6–7):

export type AuthUser = User;
export type AuthSession = Session;

User and Session come from better-auth without any options, so they're the base shapes. CloudContextVariables (runtime/handler.ts lines 29–31), AuthState, getUser, getSession and requireAuth are all typed from them.

Possible fix

Better Auth can already infer both types from the config type. This type-checks with the auth.ts above, and both role and plan are present:

import type { Auth } from 'better-auth';
import type config from './auth';

type Options = ReturnType<typeof config>;
type User = Auth<Options>['$Infer']['Session']['user']; // includes role, banned, plan
type Session = Auth<Options>['$Infer']['Session']['session']; // includes impersonatedBy

Void could generate this the way .void/env.d.ts augments ValidatedEnv and CloudBindings:

  1. Make AuthUser and AuthSession interfaces: export interface AuthUser extends User {}.
  2. Generate .void/auth.d.ts when auth.ts exists:
declare module 'void/auth' {
  interface AuthUser extends _InferredUser {}
  interface AuthSession extends _InferredSession {}
}

Everything typed from AuthUser, including CloudContextVariables, would then pick up the fields.

Workaround

Cast in one helper:

import { userTable } from '@auth-schema'; // .void/better-auth-schema.ts

type User = typeof userTable.$inferSelect;

export function getCurrentUser(c: CloudContext): User | null {
  return c.get('user') as User | null;
}

Environment

  • void 1.2.1, better-auth 1.7.11
  • TypeScript 7.0.2, vite-plus 1.3.0, Node 22.23.3, macOS 26.6
主要语言
TypeScript
星标
402
派生
32
平均合并
8 小时 32 分钟
30 天内合并 PR
111

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

voidzero-dev/void 的其他 Issue

查看 voidzero-dev/void 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。