Skip to content

Commit 5b0756c

Browse files
authored
fix(types): include auth augmentations in shared context (#310)
1 parent a862611 commit 5b0756c

7 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/module/type-templates.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ declare module '@onmax/nuxt-better-auth/config' {
111111
export function defineServerAuth<const R>(config: R & ServerAuthConfig): (ctx: _AugmentedServerAuthContext) => R
112112
}
113113
`,
114-
}, { nuxt: true, nitro: true, node: true })
114+
}, { nuxt: true, nitro: true, node: true, shared: true })
115115

116116
addTypeTemplate({
117117
filename: 'types/nuxt-better-auth-social-providers.d.ts',
@@ -128,7 +128,7 @@ declare module '#nuxt-better-auth' {
128128
}
129129
}
130130
`,
131-
}, { nuxt: true, nitro: true, node: true })
131+
}, { nuxt: true, nitro: true, node: true, shared: true })
132132

133133
addTypeTemplate({
134134
filename: 'types/nuxt-better-auth-nitro.d.ts',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
setups.@onmax/nuxt-better-auth="0.0.2-alpha.30"
1+
setups.@onmax/nuxt-better-auth="0.0.2-alpha.31"

test/cases/plugins-type-inference/server/auth.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ function customAdminLikePlugin() {
1111
},
1212
},
1313
schema: {
14+
session: {
15+
fields: {
16+
workspaceId: {
17+
type: 'string',
18+
required: false,
19+
input: false,
20+
},
21+
},
22+
},
1423
user: {
1524
fields: {
1625
role: {
@@ -27,6 +36,12 @@ function customAdminLikePlugin() {
2736
export default defineServerAuth(() => ({
2837
emailAndPassword: { enabled: true },
2938
plugins: [customAdminLikePlugin(), username()] as const,
39+
socialProviders: {
40+
github: {
41+
clientId: 'test-client-id',
42+
clientSecret: 'test-client-secret',
43+
},
44+
},
3045
user: {
3146
additionalFields: {
3247
internalCode: {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { AuthSession, AuthUser } from '#nuxt-better-auth'
2+
import type { AuthSocialProviderId } from '../../../../src/runtime/types'
3+
4+
export function assertSharedAuthTypes(user: AuthUser, session: AuthSession) {
5+
const role: string | null | undefined = user.role
6+
const internalCode: string | null | undefined = user.internalCode
7+
const workspaceId: string | null | undefined = session.workspaceId
8+
const provider: AuthSocialProviderId = 'github'
9+
10+
return {
11+
internalCode,
12+
provider,
13+
role,
14+
workspaceId,
15+
}
16+
}

test/cases/plugins-type-inference/tsconfig.type-check.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"files": [
2222
"./.nuxt/types/nuxt-better-auth-infer.d.ts",
23+
"./.nuxt/types/nuxt-better-auth-social-providers.d.ts",
2324
"./.nuxt/types/nuxt-better-auth-nitro.d.ts",
2425
"./typecheck-target.ts"
2526
]

test/cases/plugins-type-inference/typecheck-target.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { AuthUser } from '#nuxt-better-auth'
1+
import type { AuthSession, AuthUser } from '#nuxt-better-auth'
22
import type { NitroRouteRules } from 'nitropack/types'
3+
import type { AuthSocialProviderId } from '../../../src/runtime/types'
34

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

@@ -21,6 +22,18 @@ const user: AuthUser = {
2122
foo: 'bar',
2223
}
2324

25+
const session: AuthSession = {
26+
id: 'session-1',
27+
createdAt: new Date(),
28+
updatedAt: new Date(),
29+
userId: '1',
30+
expiresAt: new Date(),
31+
token: 'token',
32+
workspaceId: 'workspace-1',
33+
}
34+
35+
const provider: AuthSocialProviderId = 'github'
36+
2437
const rules: NitroRouteRules = {
2538
auth: {
2639
user: { role: 'admin', internalCode: 'x', foo: 'bar' },
@@ -34,3 +47,5 @@ void user
3447
void rules
3548
void auth
3649
void signInUsername
50+
void session
51+
void provider

test/infer-plugins-types.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,12 @@ describe('type inference regressions #107 and #192', () => {
3030
expect(output).not.toContain(`'internalCode' does not exist in type`)
3131
expect(output).not.toContain(`Property 'signInUsername' does not exist on type`)
3232
expect(output).not.toContain(`'signInUsername' does not exist on type`)
33+
34+
const sharedTypecheck = spawnSync('npx', ['vue-tsc', '--noEmit', '--pretty', 'false', '-p', '.nuxt/tsconfig.shared.json'], {
35+
cwd: fixtureDir,
36+
env,
37+
encoding: 'utf8',
38+
})
39+
expect(sharedTypecheck.status, `shared vue-tsc failed:\n${sharedTypecheck.stdout}\n${sharedTypecheck.stderr}`).toBe(0)
3340
}, 60_000)
3441
})

0 commit comments

Comments
 (0)