When you add Better Auth plugins that extend the user or session objects, TypeScript needs to know about these new fields. The module automatically infers types from your configuration, but you can also augment types manually.
Types are automatically inferred when you:
server/auth.config.tsNo additional configuration is needed for most cases.
Example:
If you add the admin plugin:
import { admin } from 'better-auth/plugins'
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
export default defineServerAuth({
plugins: [admin()]
})
You can immediately access the role property in your Vue components:
<script setup lang="ts">
const { user } = useUserSession()
// user.value.role is fully typed!
if (user.value?.role === 'admin') {
// ...
}
</script>
Manually extend types when automatic inference doesn't work:
Create a file types/auth.d.ts:
import '#nuxt-better-auth'
declare module '#nuxt-better-auth' {
interface AuthUser {
customField?: string
}
}
#nuxt-better-auth virtual module exports AuthUser and AuthSession interfaces that reflect your current configuration.Types not updating?
npx nuxi prepare to regenerate typesserver/auth.config.ts has no syntax errors