Repository navigation
fix:(cross-domain): support async storage implementations #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import type { BetterAuthClientPlugin, ClientStore } from "better-auth/client"; | ||
| import { parseSetCookieHeader } from "better-auth/cookies"; | ||
| import type { BetterFetchOption } from "@better-fetch/fetch"; | ||
| import type { crossDomain } from "./index.js"; | ||
| import { VERSION } from "../../version.js"; | ||
|
|
||
|
|
@@ -10,9 +9,9 @@ interface StoredCookie { | |
| } | ||
|
|
||
| type CrossDomainActions = { | ||
| getCookie: () => string; | ||
| getCookie: () => Promise<string>; | ||
| updateSession: () => void; | ||
| getSessionData: () => Record<string, unknown> | null; | ||
| getSessionData: () => Promise<Record<string, unknown> | null>; | ||
| }; | ||
|
|
||
| type CrossDomainClientPlugin = Omit< | ||
|
|
@@ -73,8 +72,8 @@ export function getCookie(cookie: string) { | |
| export const crossDomainClient = ( | ||
| opts: { | ||
| storage?: { | ||
| setItem: (key: string, value: string) => any; | ||
| getItem: (key: string) => string | null; | ||
| setItem: (key: string, value: string) => void | Promise<void>; | ||
| getItem: (key: string) => Promise<string | null> | string | null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/usr/bin/env bash
rg -n 'setItem\s*:|setItem\s*\(' src/plugins/cross-domainRepository: get-convex/better-auth Length of output: 1100 🏁 Script executed: #!/usr/bin/env bash
sed -n '60,280p' src/plugins/cross-domain/client.tsRepository: get-convex/better-auth Length of output: 6919 🏁 Script executed: #!/usr/bin/env bash
rg -n 'CrossDomainClientPlugin|storage\?:|setItem:\s*\(|getItem:\s*\(' src/plugins src | sed -n '1,220p'Repository: get-convex/better-auth Length of output: 1962 Type 🤖 Prompt for AI Agents |
||
| }; | ||
| storagePrefix?: string; | ||
| disableCache?: boolean; | ||
|
|
@@ -101,15 +100,15 @@ export const crossDomainClient = ( | |
| * | ||
| * @example | ||
| * ```ts | ||
| * const cookie = client.getCookie(); | ||
| * const cookie = await client.getCookie(); | ||
| * fetch("https://api.example.com", { | ||
| * headers: { | ||
| * cookie, | ||
| * }, | ||
| * }); | ||
| */ | ||
| getCookie: () => { | ||
| const cookie = storage?.getItem(cookieName); | ||
| getCookie: async () => { | ||
| const cookie = await storage?.getItem(cookieName); | ||
| return getCookie(cookie || "{}"); | ||
| }, | ||
| /** | ||
|
|
@@ -131,11 +130,11 @@ export const crossDomainClient = ( | |
| * | ||
| * @example | ||
| * ```ts | ||
| * const sessionData = client.getSessionData(); | ||
| * const sessionData = await client.getSessionData(); | ||
| * ``` | ||
| */ | ||
| getSessionData: (): Record<string, unknown> | null => { | ||
| const sessionData = storage?.getItem(localCacheName); | ||
| getSessionData: async (): Promise<Record<string, unknown> | null> => { | ||
| const sessionData = await storage?.getItem(localCacheName); | ||
| if (!sessionData) return null; | ||
| try { | ||
| const parsed = JSON.parse(sessionData); | ||
|
|
@@ -166,7 +165,7 @@ export const crossDomainClient = ( | |
| "set-better-auth-cookie" | ||
| ); | ||
| if (setCookie) { | ||
| const prevCookie = storage.getItem(cookieName); | ||
| const prevCookie = await storage.getItem(cookieName); | ||
| const toSetCookie = getSetCookie( | ||
| setCookie || "", | ||
| prevCookie ?? undefined | ||
|
Comment on lines
+168
to
171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift Serialize cookie read-modify-write operations. The new Also applies to: 210-213 🤖 Prompt for AI Agents |
||
|
|
@@ -208,7 +207,7 @@ export const crossDomainClient = ( | |
| // Previously this unconditionally set cookieName to "{}", | ||
| // which wiped the two_factor challenge token needed for | ||
| // verifyTotp in cross-domain setups. | ||
| const prev = storage.getItem(cookieName); | ||
| const prev = await storage.getItem(cookieName); | ||
| try { | ||
| const parsed = JSON.parse(prev || "{}") as Record< | ||
| string, | ||
|
|
@@ -236,11 +235,11 @@ export const crossDomainClient = ( | |
| if (!storage) { | ||
| return { | ||
| url, | ||
| options: options as BetterFetchOption, | ||
| options: options, | ||
| }; | ||
| } | ||
| options = options || {}; | ||
| const storedCookie = storage.getItem(cookieName); | ||
| const storedCookie = await storage.getItem(cookieName); | ||
| const cookie = getCookie(storedCookie || "{}"); | ||
| options.credentials = "omit"; | ||
| options.headers = { | ||
|
|
@@ -258,7 +257,7 @@ export const crossDomainClient = ( | |
| } | ||
| return { | ||
| url, | ||
| options: options as BetterFetchOption, | ||
| options: options , | ||
| }; | ||
| }, | ||
| }, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.