Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/runtime/server/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { useRuntimeConfig } from 'nitropack/runtime'
import { withoutProtocol } from 'ufo'
import { resolveCustomSecondaryStorageRequirement } from './custom-secondary-storage'

type AuthInstance = ReturnType<typeof betterAuth>
type AuthOptions = ReturnType<typeof createServerAuth>
type AuthInstance = ReturnType<typeof betterAuth<AuthOptions>>

const _authCache = new Map<string, AuthInstance>()
let _baseURLInferenceLogged = false
Expand Down
3 changes: 2 additions & 1 deletion test/cases/plugins-type-inference/server/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { username } from 'better-auth/plugins'
import { defineServerAuth } from '../../../../src/runtime/config'

function customAdminLikePlugin() {
Expand Down Expand Up @@ -25,7 +26,7 @@ function customAdminLikePlugin() {

export default defineServerAuth({
emailAndPassword: { enabled: true },
plugins: [customAdminLikePlugin()] as const,
plugins: [customAdminLikePlugin(), username()] as const,
user: {
additionalFields: {
internalCode: {
Expand Down
7 changes: 7 additions & 0 deletions test/cases/plugins-type-inference/typecheck-target.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { AuthUser } from '#nuxt-better-auth'
import type { NitroRouteRules } from 'nitropack/types'

declare const serverAuth: typeof import('../../../src/runtime/server/utils/auth').serverAuth

declare module '#nuxt-better-auth' {
interface AuthUser {
foo: string
Expand All @@ -25,5 +27,10 @@ const rules: NitroRouteRules = {
},
}

const auth = serverAuth()
const signInUsername = auth.api.signInUsername

void user
void rules
void auth
void signInUsername
6 changes: 4 additions & 2 deletions test/infer-plugins-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const env = {
BETTER_AUTH_SECRET: 'test-secret-for-testing-only-32chars',
}

describe('type inference regression #107', () => {
it('typechecks routeRules user fields inferred from plugins/additionalFields', () => {
describe('type inference regressions #107 and #192', () => {
it('typechecks plugin/additional fields and serverAuth plugin API inference', () => {
const prepare = spawnSync('npx', ['nuxi', 'prepare'], {
cwd: fixtureDir,
env,
Expand All @@ -28,5 +28,7 @@ describe('type inference regression #107', () => {
expect(output).not.toContain(`is not assignable to type 'BetterAuthPlugin'`)
expect(output).not.toContain(`'role' does not exist in type`)
expect(output).not.toContain(`'internalCode' does not exist in type`)
expect(output).not.toContain(`Property 'signInUsername' does not exist on type`)
expect(output).not.toContain(`'signInUsername' does not exist on type`)
}, 60_000)
})
Loading