64 lines
1.4 KiB
TypeScript
64 lines
1.4 KiB
TypeScript
import { ConvexError, type Value } from "convex/values"
|
|
|
|
const AUTH_ERROR_MESSAGES = new Set([
|
|
"Not authenticated",
|
|
"User not logged in",
|
|
])
|
|
|
|
function matchesAuthMessage(message: string): boolean {
|
|
if (AUTH_ERROR_MESSAGES.has(message)) {
|
|
return true
|
|
}
|
|
|
|
const normalized = message.toLowerCase()
|
|
return (
|
|
normalized.includes("not authenticated") ||
|
|
normalized.includes("unauthenticated") ||
|
|
normalized.includes("invalid session") ||
|
|
normalized.includes("invalid token") ||
|
|
normalized.includes("auth error")
|
|
)
|
|
}
|
|
|
|
function convexErrorData(error: ConvexError<Value>): string | null {
|
|
if (typeof error.data === "string") {
|
|
return error.data
|
|
}
|
|
return null
|
|
}
|
|
|
|
export function isConvexTransportAuthFailure(error: unknown): boolean {
|
|
if (!(error instanceof Error)) {
|
|
return false
|
|
}
|
|
|
|
return (
|
|
error.message.includes("[CONVEX") && error.message.includes("Server Error")
|
|
)
|
|
}
|
|
|
|
export function isConvexAuthError(error: unknown): boolean {
|
|
if (error instanceof ConvexError) {
|
|
const data = convexErrorData(error)
|
|
if (data && matchesAuthMessage(data)) {
|
|
return true
|
|
}
|
|
if (matchesAuthMessage(error.message)) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return isConvexTransportAuthFailure(error)
|
|
}
|
|
|
|
export function shouldInvalidateAuthSession(
|
|
error: unknown,
|
|
sessionEstablished: boolean
|
|
): boolean {
|
|
if (!sessionEstablished) {
|
|
return false
|
|
}
|
|
|
|
return isConvexAuthError(error)
|
|
}
|