Infer `AuthUser` / `AuthSession` from `auth.ts`: plugin and `additionalFields` columns are missing from `c.get('user')`, `getUser()` and `requireAuth()`
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 53/100
- Tipo de issue
- Bug
- Clareza
- Razoavelmente clara
- Status de atividade
- Ativa
- Stack de tecnologia
- typescript
- Domínio
- backend, developer-experience
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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
useronCloudContextVariablesfails with TS2717, because the property already has a different type. AuthUseris 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
-
Enable auth and add
auth.tswith 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 } } }, })); -
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, })); -
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:
- Make
AuthUserandAuthSessioninterfaces:export interface AuthUser extends User {}. - Generate
.void/auth.d.tswhenauth.tsexists:
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
- Linguagem predominante
- TypeScript
- Estrelas
- 402
- Forks
- 32
- Merge médio
- 8h 32min
- PRs com merge (30d)
- 111
Preparar o ambiente
Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de voidzero-dev/void
-
Dev: `better-auth/client/plugins` fails to resolve when auth is enabled (the `better-auth/client` alias matches it as a prefix)Talvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
voidzero-dev/void#565 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
voidzero-dev/void#529 ·
Mantenedores costumam responder em até 1 dia
-
bug
Dificuldade 1/5 1-3 horas Facilidade para iniciantes 78/100
voidzero-dev/void#428 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 55/100
voidzero-dev/void#564 ·
Mantenedores costumam responder em até 1 dia
-
Navigation commit failure leaves the loading state active and causes inconsistent internal stateAberta
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 48/100
voidzero-dev/void#562 ·
Mantenedores costumam responder em até 1 dia
Todas as issues de voidzero-dev/void
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
farion1231/cc-switch#8829 ·
Mantenedores costumam responder em até 1 dia
-
[core-client] Remove unused hand-maintained transport helpers after the definition-table cutoverAbertadoc-audit enhancement
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
-
Snapshot capture failure (EPERM spawning git) fails the whole step despite the best-effort contractAbertareproduced
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
anomalyco/opencode#58685 · 1 comentário ·
Mantenedores costumam responder em até 1 dia