Skip to content

Commit 8a05dab

Browse files
committed
fix: auto whitelist urls on register
1 parent 50e22d5 commit 8a05dab

File tree

2 files changed

+97
-11
lines changed

2 files changed

+97
-11
lines changed

src/module.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,20 @@ export default defineNuxtModule<RuntimeModuleOptions>({
7272
auth: {
7373
whitelist: (() => {
7474
const combinedWhitelist = [...(defaultOptions.auth?.whitelist || []), ...(options.auth?.whitelist || [])]
75-
// Auto-whitelist /confirm-email if /register is whitelisted
76-
if (combinedWhitelist.includes('/register') && !combinedWhitelist.includes('/confirm-email')) {
77-
combinedWhitelist.push('/confirm-email')
75+
// Auto-whitelist related endpoints if /register is whitelisted
76+
if (combinedWhitelist.includes('/register')) {
77+
const apiBasePath = options.apiBasePath || defaultOptions.apiBasePath
78+
const registrationEndpoints = [
79+
'/confirm-email', // Page route for email confirmation
80+
`${apiBasePath}/register`, // API endpoint for registration
81+
`${apiBasePath}/confirm-email` // API endpoint for email confirmation
82+
]
83+
84+
registrationEndpoints.forEach(endpoint => {
85+
if (!combinedWhitelist.includes(endpoint)) {
86+
combinedWhitelist.push(endpoint)
87+
}
88+
})
7889
}
7990
return combinedWhitelist
8091
})(),
@@ -98,9 +109,20 @@ export default defineNuxtModule<RuntimeModuleOptions>({
98109
auth: {
99110
whitelist: (() => {
100111
const combinedWhitelist = [...(defaultOptions.auth?.whitelist || []), ...(options.auth?.whitelist || [])]
101-
// Auto-whitelist /confirm-email if /register is whitelisted
102-
if (combinedWhitelist.includes('/register') && !combinedWhitelist.includes('/confirm-email')) {
103-
combinedWhitelist.push('/confirm-email')
112+
// Auto-whitelist related endpoints if /register is whitelisted
113+
if (combinedWhitelist.includes('/register')) {
114+
const apiBasePath = options.apiBasePath || defaultOptions.apiBasePath
115+
const registrationEndpoints = [
116+
'/confirm-email', // Page route for email confirmation
117+
`${apiBasePath}/register`, // API endpoint for registration
118+
`${apiBasePath}/confirm-email` // API endpoint for email confirmation
119+
]
120+
121+
registrationEndpoints.forEach(endpoint => {
122+
if (!combinedWhitelist.includes(endpoint)) {
123+
combinedWhitelist.push(endpoint)
124+
}
125+
})
104126
}
105127
return combinedWhitelist
106128
})(),

test/unit/auto-whitelist.test.ts

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,35 @@ interface TestModuleOptions {
55
auth: {
66
whitelist: string[]
77
}
8+
apiBasePath?: string
89
}
910

10-
// Extract the whitelist logic for testing
11-
const getWhitelistWithAutoConfirmEmail = (options: TestModuleOptions) => {
11+
// Extract the whitelist logic for testing (matching the module.ts logic)
12+
const getWhitelistWithAutoRegistrationEndpoints = (options: TestModuleOptions) => {
1213
const combinedWhitelist = [...options.auth.whitelist]
13-
// Auto-whitelist /confirm-email if /register is whitelisted
14-
if (combinedWhitelist.includes('/register') && !combinedWhitelist.includes('/confirm-email')) {
15-
combinedWhitelist.push('/confirm-email')
14+
// Auto-whitelist related endpoints if /register is whitelisted
15+
if (combinedWhitelist.includes('/register')) {
16+
const apiBasePath = options.apiBasePath || '/api/nuxt-users'
17+
const registrationEndpoints = [
18+
'/confirm-email', // Page route for email confirmation
19+
`${apiBasePath}/register`, // API endpoint for registration
20+
`${apiBasePath}/confirm-email` // API endpoint for email confirmation
21+
]
22+
23+
registrationEndpoints.forEach(endpoint => {
24+
if (!combinedWhitelist.includes(endpoint)) {
25+
combinedWhitelist.push(endpoint)
26+
}
27+
})
1628
}
1729
return combinedWhitelist
1830
}
1931

32+
// Keep backward compatibility helper for existing tests
33+
const getWhitelistWithAutoConfirmEmail = (options: TestModuleOptions) => {
34+
return getWhitelistWithAutoRegistrationEndpoints(options)
35+
}
36+
2037
describe('Auto-whitelist functionality', () => {
2138
it('should auto-add /confirm-email when /register is whitelisted', () => {
2239
const options: TestModuleOptions = {
@@ -70,4 +87,51 @@ describe('Auto-whitelist functionality', () => {
7087
expect(result).not.toContain('/confirm-email')
7188
expect(result).toHaveLength(0)
7289
})
90+
91+
it('should auto-add registration API endpoints when /register is whitelisted', () => {
92+
const options: TestModuleOptions = {
93+
auth: {
94+
whitelist: ['/register']
95+
}
96+
}
97+
98+
const result = getWhitelistWithAutoRegistrationEndpoints(options)
99+
100+
expect(result).toContain('/register')
101+
expect(result).toContain('/confirm-email')
102+
expect(result).toContain('/api/nuxt-users/register')
103+
expect(result).toContain('/api/nuxt-users/confirm-email')
104+
expect(result).toHaveLength(4)
105+
})
106+
107+
it('should use custom API base path for registration endpoints', () => {
108+
const options: TestModuleOptions = {
109+
auth: {
110+
whitelist: ['/register']
111+
},
112+
apiBasePath: '/api/custom-users'
113+
}
114+
115+
const result = getWhitelistWithAutoRegistrationEndpoints(options)
116+
117+
expect(result).toContain('/register')
118+
expect(result).toContain('/confirm-email')
119+
expect(result).toContain('/api/custom-users/register')
120+
expect(result).toContain('/api/custom-users/confirm-email')
121+
expect(result).not.toContain('/api/nuxt-users/register')
122+
})
123+
124+
it('should not duplicate API endpoints if already present', () => {
125+
const options: TestModuleOptions = {
126+
auth: {
127+
whitelist: ['/register', '/api/nuxt-users/register']
128+
}
129+
}
130+
131+
const result = getWhitelistWithAutoRegistrationEndpoints(options)
132+
133+
expect(result.filter(route => route === '/api/nuxt-users/register')).toHaveLength(1)
134+
expect(result).toContain('/confirm-email')
135+
expect(result).toContain('/api/nuxt-users/confirm-email')
136+
})
73137
})

0 commit comments

Comments
 (0)