Skip to content

Commit d7e6c61

Browse files
committed
test: fix ts errors
1 parent a206962 commit d7e6c61

File tree

8 files changed

+41
-24
lines changed

8 files changed

+41
-24
lines changed

playground/nuxt.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export default defineNuxtConfig({
1616
admin: ['*'],
1717
}, */
1818
google: {
19-
clientId: process.env.GOOGLE_CLIENT_ID || 'demo-client-id',
20-
clientSecret: process.env.GOOGLE_CLIENT_SECRET || 'demo-client-secret',
19+
clientId: process.env.GOOGLE_CLIENT_ID || 'google-client-id',
20+
clientSecret: process.env.GOOGLE_CLIENT_SECRET || 'google-client-secret',
2121
successRedirect: '/',
2222
errorRedirect: '/login?error=oauth_failed'
2323
}

src/module.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ export default defineNuxtModule<RuntimeModuleOptions>({
6060

6161
// Add runtime config (server-side)
6262
// Priority: runtimeConfig (strongest) > top-level app config > default options (weakest)
63-
const runtimeConfigOptions = defu(nuxt.options.runtimeConfig.nuxtUsers || {}, options, defaultOptions)
63+
const runtimeConfigOptions = defu(nuxt.options.runtimeConfig.nuxtUsers || {}, options, defaultOptions) as ModuleOptions
6464

6565
nuxt.options.runtimeConfig.nuxtUsers = {
6666
...runtimeConfigOptions,
6767
auth: {
68-
...runtimeConfigOptions.auth,
68+
tokenExpiration: runtimeConfigOptions.auth.tokenExpiration,
69+
rememberMeExpiration: runtimeConfigOptions.auth.rememberMeExpiration,
70+
permissions: runtimeConfigOptions.auth.permissions,
71+
...(runtimeConfigOptions.auth.google && { google: runtimeConfigOptions.auth.google }),
6972
whitelist: (() => {
7073
const combinedWhitelist = [...(defaultOptions.auth?.whitelist || []), ...(runtimeConfigOptions.auth?.whitelist || [])]
7174
const apiBasePath = runtimeConfigOptions.apiBasePath || defaultOptions.apiBasePath
@@ -102,7 +105,7 @@ export default defineNuxtModule<RuntimeModuleOptions>({
102105
return combinedWhitelist
103106
})()
104107
},
105-
}
108+
} as unknown as typeof nuxt.options.runtimeConfig.nuxtUsers
106109

107110
// Add public runtime config for client-side access
108111
nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {}

src/runtime/components/NUsersGoogleLoginButton.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import { ref, computed } from 'vue'
33
import type { ModuleOptions } from 'nuxt-users/utils'
4-
import { useRuntimeConfig } from '#imports'
4+
import { useRuntimeConfig, navigateTo } from '#imports'
55
66
interface Props {
77
/**

src/runtime/middleware/authorization.client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
3030
const { isAuthenticated, user, fetchUser } = useAuthentication()
3131

3232
// If not authenticated but oauth_success flag is present, fetch user using SSR
33-
if (!isAuthenticated.value && to.query.oauth_success === 'true') {
33+
if (!isAuthenticated.value && to.query?.oauth_success === 'true') {
3434
try {
3535
await fetchUser(true) // Use SSR to properly handle httpOnly cookies
3636
}

src/runtime/server/utils/google-oauth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function getGoogleUserFromCode(oauth2Client: any, code: string): Pr
5454
id: data.id!,
5555
email: data.email,
5656
name: data.name || data.email.split('@')[0],
57-
picture: data.picture,
57+
picture: data.picture || undefined,
5858
verified_email: data.verified_email
5959
}
6060
}

test/cli/migrate.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,23 @@ describe('CLI: Migrate', () => {
7979
expect(appliedMigrations).toContain('create_users_table')
8080
expect(appliedMigrations).toContain('create_personal_access_tokens_table')
8181
expect(appliedMigrations).toContain('create_password_reset_tokens_table')
82-
expect(appliedMigrations).toHaveLength(5)
82+
expect(appliedMigrations).toContain('add_active_to_users')
83+
expect(appliedMigrations).toContain('add_google_oauth_fields')
84+
expect(appliedMigrations).toHaveLength(6)
8385
})
8486

8587
it('should not run migrations twice', async () => {
8688
// Run migrations first time
8789
await runMigrations(testOptions)
8890

8991
const firstRunMigrations = await getAppliedMigrations(testOptions)
90-
expect(firstRunMigrations).toHaveLength(5)
92+
expect(firstRunMigrations).toHaveLength(6)
9193

9294
// Run migrations second time
9395
await runMigrations(testOptions)
9496

9597
const secondRunMigrations = await getAppliedMigrations(testOptions)
96-
expect(secondRunMigrations).toHaveLength(5)
98+
expect(secondRunMigrations).toHaveLength(6)
9799

98100
// Should be the same migrations
99101
expect(secondRunMigrations).toEqual(firstRunMigrations)
@@ -184,12 +186,13 @@ describe('CLI: Migrate', () => {
184186
// Test migrations table structure
185187
const migrationsResult2 = await db.sql`SELECT id, name, executed_at FROM migrations ORDER BY id`
186188
const migrations = migrationsResult2.rows || []
187-
expect(migrations).toHaveLength(5)
189+
expect(migrations).toHaveLength(6)
188190
expect(migrations[0].name).toBe('create_migrations_table')
189191
expect(migrations[1].name).toBe('create_users_table')
190192
expect(migrations[2].name).toBe('create_personal_access_tokens_table')
191193
expect(migrations[3].name).toBe('create_password_reset_tokens_table')
192194
expect(migrations[4].name).toBe('add_active_to_users')
195+
expect(migrations[5].name).toBe('add_google_oauth_fields')
193196
})
194197

195198
it('should handle partial migrations correctly', async () => {
@@ -233,6 +236,7 @@ describe('CLI: Migrate', () => {
233236
expect(appliedMigrations).toContain('create_personal_access_tokens_table')
234237
expect(appliedMigrations).toContain('create_password_reset_tokens_table')
235238
expect(appliedMigrations).toContain('add_active_to_users')
236-
expect(appliedMigrations).toHaveLength(5)
239+
expect(appliedMigrations).toContain('add_google_oauth_fields')
240+
expect(appliedMigrations).toHaveLength(6)
237241
})
238242
})

test/google-oauth-flow.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,12 @@ describe('Google OAuth Complete Flow Integration', () => {
228228
// User will be found (returns user object)
229229
const user = await findOrCreateGoogleUser(mockGoogleUser, testOptions)
230230
expect(user).toBeDefined()
231-
expect(user?.active).toBeFalsy()
231+
expect(user).not.toBeNull()
232+
expect(user!.active).toBeFalsy()
232233

233234
// The callback handler should reject this user
234235
// Simulating what happens in callback.get.ts
235-
if (!user?.active) {
236+
if (user && !user.active) {
236237
// Should redirect to error page instead of creating token
237238
expect(user.active).toBeFalsy()
238239
// No token should be created - verify

test/unit/middleware.google-oauth.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ describe('Google OAuth Client Middleware', () => {
9494
query: { oauth_success: 'true' }
9595
}
9696

97-
await authorizationMiddleware.default(to as any)
97+
const from = { path: '/login' } as any
98+
await authorizationMiddleware.default(to as any, from)
9899

99100
// Should call fetchUser with SSR enabled
100101
expect(mockFetchUser).toHaveBeenCalledWith(true)
@@ -129,7 +130,8 @@ describe('Google OAuth Client Middleware', () => {
129130
query: { oauth_success: 'true' }
130131
}
131132

132-
await authorizationMiddleware.default(to as any)
133+
const from = { path: '/login' } as any
134+
await authorizationMiddleware.default(to as any, from)
133135

134136
expect(mockFetchUser).toHaveBeenCalledWith(true)
135137
// After fetch, should NOT redirect to login
@@ -150,7 +152,8 @@ describe('Google OAuth Client Middleware', () => {
150152
query: { oauth_success: 'true' }
151153
}
152154

153-
await authorizationMiddleware.default(to as any)
155+
const from = { path: '/login' } as any
156+
await authorizationMiddleware.default(to as any, from)
154157

155158
expect(mockFetchUser).toHaveBeenCalledWith(true)
156159
// After failed fetch, should redirect to login
@@ -177,7 +180,8 @@ describe('Google OAuth Client Middleware', () => {
177180
query: { oauth_success: 'true' }
178181
}
179182

180-
await authorizationMiddleware.default(to as any)
183+
const from = { path: '/login' } as any
184+
await authorizationMiddleware.default(to as any, from)
181185

182186
// Should not fetch user if already authenticated
183187
expect(mockFetchUser).not.toHaveBeenCalled()
@@ -199,7 +203,8 @@ describe('Google OAuth Client Middleware', () => {
199203
query: {}
200204
}
201205

202-
await authorizationMiddleware.default(to as any)
206+
const from = { path: '/login' } as any
207+
await authorizationMiddleware.default(to as any, from)
203208

204209
// Should NOT fetch user without oauth_success flag
205210
expect(mockFetchUser).not.toHaveBeenCalled()
@@ -223,7 +228,8 @@ describe('Google OAuth Client Middleware', () => {
223228
query: { oauth_success: 'true' }
224229
}
225230

226-
await authorizationMiddleware.default(to as any)
231+
const from = { path: '/login' } as any
232+
await authorizationMiddleware.default(to as any, from)
227233

228234
// Should call with true to enable SSR (critical for httpOnly cookies)
229235
expect(mockFetchUser).toHaveBeenCalledWith(true)
@@ -254,7 +260,8 @@ describe('Google OAuth Client Middleware', () => {
254260
query: {}
255261
}
256262

257-
const result = await authorizationMiddleware.default(to as any)
263+
const from = { path: '/login' } as any
264+
const result = await authorizationMiddleware.default(to as any, from)
258265

259266
expect(result).toBeUndefined() // Middleware allows passage
260267
expect(mockNavigateTo).not.toHaveBeenCalled()
@@ -283,7 +290,8 @@ describe('Google OAuth Client Middleware', () => {
283290
query: {}
284291
}
285292

286-
await authorizationMiddleware.default(to as any)
293+
const from = { path: '/login' } as any
294+
await authorizationMiddleware.default(to as any, from)
287295

288296
expect(mockNavigateTo).toHaveBeenCalledWith('/login')
289297
})
@@ -305,7 +313,8 @@ describe('Google OAuth Client Middleware', () => {
305313
query: { oauth_success: 'true' }
306314
}
307315

308-
const result = await authorizationMiddleware.default(to as any)
316+
const from = { path: '/login' } as any
317+
const result = await authorizationMiddleware.default(to as any, from)
309318

310319
// Should not fetch user or redirect
311320
expect(mockFetchUser).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)