40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { authStorageKey, secureGetItem } from "@/lib/auth-storage"
|
|
|
|
const JWT_STORAGE_KEY = "__convexAuthJWT"
|
|
const REFRESH_STORAGE_KEY = "__convexAuthRefreshToken"
|
|
|
|
function isConvexAuthLocalHost(host: string) {
|
|
return /(localhost|127\.0\.0\.1):\d+/.test(host)
|
|
}
|
|
|
|
function authCookieNames(serverUrl: string) {
|
|
const host = new URL(serverUrl).host
|
|
const prefix = isConvexAuthLocalHost(host) ? "" : "__Host-"
|
|
return {
|
|
jwt: `${prefix}${JWT_STORAGE_KEY}`,
|
|
refresh: `${prefix}${REFRESH_STORAGE_KEY}`,
|
|
}
|
|
}
|
|
|
|
export async function getAuthCookieHeader(
|
|
serverUrl: string,
|
|
convexUrl: string
|
|
): Promise<string | null> {
|
|
const [jwt, refreshToken] = await Promise.all([
|
|
secureGetItem(authStorageKey(JWT_STORAGE_KEY, convexUrl)),
|
|
secureGetItem(authStorageKey(REFRESH_STORAGE_KEY, convexUrl)),
|
|
])
|
|
|
|
if (!jwt) {
|
|
return null
|
|
}
|
|
|
|
const names = authCookieNames(serverUrl)
|
|
const parts = [`${names.jwt}=${jwt}`]
|
|
if (refreshToken) {
|
|
parts.push(`${names.refresh}=${refreshToken}`)
|
|
}
|
|
|
|
return parts.join("; ")
|
|
}
|