fix(passkey): atom listeners not working (#5096)

This commit is contained in:
Maxwell
2025-10-08 05:10:09 +10:00
committed by GitHub
parent 613747ef8e
commit 57fcdfad08
2 changed files with 29 additions and 16 deletions

View File

@@ -93,12 +93,14 @@ export function createDynamicPathProxy<T extends Record<string, any>>(
method,
async onSuccess(context) {
await options?.onSuccess?.(context);
if (!atomListeners) return;
/**
* We trigger listeners
*/
const matches = atomListeners?.find((s) => s.matcher(routePath));
if (!matches) return;
const signal = atoms[matches.signal as any];
const matches = atomListeners.filter((s) => s.matcher(routePath));
if (!matches.length) return;
for (const match of matches) {
const signal = atoms[match.signal as any];
if (!signal) return;
/**
* To avoid race conditions we set the signal in a setTimeout
@@ -108,6 +110,7 @@ export function createDynamicPathProxy<T extends Record<string, any>>(
//@ts-expect-error
signal.set(!val);
}, 10);
}
},
});
},

View File

@@ -11,7 +11,7 @@ import type {
import type { Session } from "inspector";
import type { User } from "../../types";
import type { passkey as passkeyPl, Passkey } from ".";
import type { BetterAuthClientPlugin } from "../../client/types";
import type { BetterAuthClientPlugin, Store } from "../../client/types";
import { useAuthQuery } from "../../client";
import { atom } from "nanostores";
@@ -19,8 +19,10 @@ export const getPasskeyActions = (
$fetch: BetterFetch,
{
$listPasskeys,
$store,
}: {
$listPasskeys: ReturnType<typeof atom<any>>;
$store: Store;
},
) => {
const signInPasskey = async (
@@ -55,6 +57,8 @@ export const getPasskeyActions = (
...options,
method: "POST",
});
$listPasskeys.set(Math.random());
$store.notify("$sessionSignal");
return verified;
} catch (e) {
@@ -204,9 +208,10 @@ export const passkeyClient = () => {
return {
id: "passkey",
$InferServerPlugin: {} as ReturnType<typeof passkeyPl>,
getActions: ($fetch) =>
getActions: ($fetch, $store) =>
getPasskeyActions($fetch, {
$listPasskeys,
$store,
}),
getAtoms($fetch) {
const listPasskeys = useAuthQuery<Passkey[]>(
@@ -232,10 +237,15 @@ export const passkeyClient = () => {
return (
path === "/passkey/verify-registration" ||
path === "/passkey/delete-passkey" ||
path === "/passkey/update-passkey"
path === "/passkey/update-passkey" ||
path === "/sign-out"
);
},
signal: "_listPasskeys",
signal: "$listPasskeys",
},
{
matcher: (path) => path === "/passkey/verify-authentication",
signal: "$sessionSignal",
},
],
} satisfies BetterAuthClientPlugin;