Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/fix-auth-js-absolute-basepath.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/auth-js': patch
---

Handle absolute URL in basePath of AuthConfigManager.setConfig()
57 changes: 57 additions & 0 deletions packages/auth-js/src/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, it, expect } from 'vitest'
import { normalizeBasePath } from './client'

describe('normalizeBasePath', () => {
it('should split absolute http URL into baseUrl and basePath', () => {
const result = normalizeBasePath({ baseUrl: '', basePath: 'http://localhost:8000/api/auth' })
expect(result.baseUrl).toBe('http://localhost:8000')
expect(result.basePath).toBe('/api/auth')
expect(`${result.baseUrl}${result.basePath}/session`).toBe(
'http://localhost:8000/api/auth/session'
)
})

it('should split absolute https URL into baseUrl and basePath', () => {
const result = normalizeBasePath({ baseUrl: '', basePath: 'https://example.com/auth' })
expect(result.baseUrl).toBe('https://example.com')
expect(result.basePath).toBe('/auth')
expect(`${result.baseUrl}${result.basePath}/session`).toBe('https://example.com/auth/session')
})

it('should not modify relative basePath', () => {
const result = normalizeBasePath({ baseUrl: 'http://localhost:3000', basePath: '/custom/auth' })
expect(result.baseUrl).toBe('http://localhost:3000')
expect(result.basePath).toBe('/custom/auth')
expect(`${result.baseUrl}${result.basePath}/session`).toBe(
'http://localhost:3000/custom/auth/session'
)
})

it('should handle absolute URL without path', () => {
const result = normalizeBasePath({ baseUrl: '', basePath: 'http://localhost:8000' })
expect(result.baseUrl).toBe('http://localhost:8000')
expect(result.basePath).toBe('')
expect(`${result.baseUrl}${result.basePath}/session`).toBe('http://localhost:8000/session')
})

it('should override baseUrl when both are absolute URLs', () => {
const result = normalizeBasePath({
baseUrl: 'http://localhost:3000',
basePath: 'http://localhost:8000/api/auth',
})
expect(result.baseUrl).toBe('http://localhost:8000')
expect(result.basePath).toBe('/api/auth')
expect(`${result.baseUrl}${result.basePath}/session`).toBe(
'http://localhost:8000/api/auth/session'
)
})

it('should handle absolute URL with trailing slash', () => {
const result = normalizeBasePath({ baseUrl: '', basePath: 'http://localhost:8000/api/auth/' })
expect(result.baseUrl).toBe('http://localhost:8000')
expect(result.basePath).toBe('/api/auth')
expect(`${result.baseUrl}${result.basePath}/session`).toBe(
'http://localhost:8000/api/auth/session'
)
})
})
12 changes: 12 additions & 0 deletions packages/auth-js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ interface ParsedUrl {
toString: () => string
}

export function normalizeBasePath<T extends { basePath?: string }>(config: T): T {
if (config.basePath && /^https?:\/\//.test(config.basePath)) {
const url = new URL(config.basePath)
return {
...config,
baseUrl: url.origin,
basePath: url.pathname.replace(/\/$/, ''),
}
}
return config
}

export function parseUrl(url?: string): ParsedUrl {
const defaultUrl = 'http://localhost:3000/api/auth'
const parsedUrl = new URL(url ? (url.startsWith('http') ? url : `https://${url}`) : defaultUrl)
Expand Down
12 changes: 10 additions & 2 deletions packages/auth-js/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import type { BuiltInProviderType, RedirectableProviderType } from '@auth/core/p
import type { LoggerInstance, Session } from '@auth/core/types'
import * as React from 'react'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { ClientSessionError, fetchData, now, parseUrl, useOnline } from './client'
import {
ClientSessionError,
fetchData,
normalizeBasePath,
now,
parseUrl,
useOnline,
} from './client'
import type {
WindowProps,
AuthState,
Expand Down Expand Up @@ -53,7 +60,8 @@ class AuthConfigManager {
}

setConfig(userConfig: Partial<AuthClientConfig>): void {
this.config = { ...this.config, ...userConfig }
const normalized = normalizeBasePath(userConfig)
this.config = { ...this.config, ...normalized }
}

getConfig(): AuthClientConfig {
Expand Down